您的位置:首页 > 运维架构 > Shell

shell programing

2015-08-29 14:23 453 查看
$ /bin/bash –version

Pipes and Redirection

$ ls -l > lsoutput.txt

file descriptor 0 is the standard input

file descriptor 1 is the standard output

file descriptor 2 is the standard error

set cannot overwrite existing file

$ set -o noclobber

cancel

$ set +o noclobber

append to the file

$ ps >> lsoutput.txt

examples:

$ kill -HUP 1234 >killout.txt 2>killerr.txt

$ kill -1 1234 >killouterr.txt 2>&1

$ kill -1 1234 >/dev/null 2>&1

$ more < killout.txt

$ ps | sort > pssort.out

never use the same filename twice in a string of commands.

cat mydata.txt | sort | uniq > mydata.txt

Interactive Programs

$ for file in *
> do
> if grep -l POSIX $file
> then
> more $file
> fi
> done
posix
This is a file with POSIX in it - treat it well
$


通配符:

[set] – single character , [^set] negates the set

list the files my_fingers and my_toes

ls my_{finger,toe}s

$ more `grep -l POSIX *`
$ more $(grep -l POSIX *)
/* file contain POSIX */
$ grep -l POSIX * | more


Creating a Script

the #! characters tell the system that the argument that follows on the line is the program to be used to execute this file. In this case, /bin/sh is the default shell program.

filename : first

#!/bin/sh

# first
# This file looks through all the files int the current
# directory for the string POSIX, and then prints the names of
# those files to the standard output.

for file in *
do
if grep -q POSIX $file
then
echo $file
fi
done

exit 0


check if they are scripts or not

$ file first

first: POSIX shell script, ASCII text executable

Making a Script Executable

$ /bin/sh first

$ chmod +x first

either type PATH=$PATH:. on the command line or edit your .bash_profile file to add this command to the end of the file; then log out and log back in again. Alternatively, type ./first in the directory.

for your own use, you could create a bin directory in your home directory and add that to your path.

If you want the script to be executable by others, you could use /usr/local/bin or another system directory as a convenient location for adding new programs.

be safeful use

# cp first /usr/local/bin
# chown root /usr/local/bin/first
# chgrp root /usr/local/bin/first
# chmod 755 /usr/local/bin/first
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: