$ echo '#!/bin/sh' > my-script.sh$ echo 'echo Hello World' >> my-script.sh$ chmod 755 my-script.sh$ ./my-script.shHello World$
上面这段代码中,echo
是打印的意思,而>
是重定向的意思,chmod
是修改权限的意思。shell
脚本以.sh
为结尾。
#!/bin/sh# This is a comment!echo Hello World # This is a comment, too!
以上为my-script.sh
中的代码,可以学习一下如何写注释。
$ chmod a+rx my-script.sh$ ./my-script.sh
想要将shell
脚本变成可执行的,可以使用以上语句。
grep "mystring" /tmp/myfile
这句的意思是将在/tmp/myfile
中的"mystring"
字符串搜索出来。
#!/bin/sh# This is a comment!echo Hello World # This is a comment, too!
以上是first.sh
中的代码。可以使用以下代码执行:
$ chmod 755 first.sh$ ./first.shHello World$
得到结果:
$ echo Hello WorldHello World$
#!/bin/shMY_MESSAGE="Hello World"echo $MY_MESSAGE
以上是var.sh
的代码。用来学习变量的使用。环境变量使用以下代码实现:
$ export name=zuoyuan
#!/bin/shecho What is your name?read MY_NAMEecho "Hello $MY_NAME - hope you're well."
以上是var2.sh
的代码。
#!/bin/shecho "What is your name?"read USER_NAMEecho "Hello $USER_NAME"echo "I will create you a file called ${USER_NAME}_file"touch "${USER_NAME}_file"
以上代码可以创建一个文件,touch
关键字的作用是如果有这个文件,不做任何操作,如果没有这个文件,就创建一个这个文件。
#!/bin/shfor i in 1 2 3 4 5do echo "Looping ... number $i"done
以上是for
循环。写入for.sh
。
#!/bin/shfor i in hello 1 * 2 goodbyedo echo "Looping ... i is set to $i"done
继续for
循环。写入for2.sh
中。
#!/bin/shINPUT_STRING=hellowhile [ "$INPUT_STRING" != "bye" ]do echo "Please type something in (bye to quit)" read INPUT_STRING echo "You typed: $INPUT_STRING"done
while
循环。
#!/bin/shwhile :do echo "Please type something in (^C to quit)" read INPUT_STRING echo "You typed: $INPUT_STRING"done
继续while
循环。
#!/bin/shwhile read fdo case $f in hello) echo English ;; howdy) echo American ;; gday) echo Australian ;; bonjour) echo French ;; "guten tag") echo German ;; *) echo Unknown Language: $f ;; esacdone < myfile
使用while
循环读取文件。
for runlevel in 0 1 2 3 4 5 6 Sdo mkdir rc${runlevel}.ddone
批量建文件。
if [ ... ]then # if-codeelse # else-codefi
if [ ... ]; then # do somethingfi
if [ something ]; then echo "Something" elif [ something_else ]; then echo "Something else" else echo "None of the above"fi
以上是几种条件结构。
#!/bin/shif [ "$X" -lt "0" ]then echo "X is less than zero"fiif [ "$X" -gt "0" ]; then echo "X is more than zero"fi[ "$X" -le "0" ] && \ echo "X is less than or equal to zero"[ "$X" -ge "0" ] && \ echo "X is more than or equal to zero"[ "$X" = "0" ] && \ echo "X is the string or number \"0\""[ "$X" = "hello" ] && \ echo "X matches the string \"hello\""[ "$X" != "hello" ] && \ echo "X is not the string \"hello\""[ -n "$X" ] && \ echo "X is of nonzero length"[ -f "$X" ] && \ echo "X is the path of a real file" || \ echo "No such file: $X"[ -x "$X" ] && \ echo "X is the path of an executable file"[ "$X" -nt "/etc/passwd" ] && \ echo "X is a file which is newer than /etc/passwd"
echo -en "Please guess the magic number: "read Xecho $X | grep "[^0-9]" > /dev/null 2>&1if [ "$?" -eq "0" ]; then # If the grep found something other than 0-9 # then it's not an integer. echo "Sorry, wanted a number"else # The grep found only 0-9, so it's an integer. # We can safely do a test on it. if [ "$X" -eq "7" ]; then echo "You entered the magic number!" fifi
#!/bin/shX=0while [ -n "$X" ]do echo "Enter some text (RETURN to quit)" read X echo "You said: $X"done
#!/bin/shX=0while [ -n "$X" ]do echo "Enter some text (RETURN to quit)" read X if [ -n "$X" ]; then echo "You said: $X" fidone
if [ "$X" -lt "0" ]then echo "X is less than zero"fi.......... and ........if [ ! -n "$X" ]; then echo "You said: $X"fi
以上是几个条件结构的例子。
继续举几个例子。
#!/bin/shecho "Please talk to me ..."while :do read INPUT_STRING case $INPUT_STRING in hello) echo "Hello yourself!" ;; bye) echo "See you again!" break ;; *) echo "Sorry, I don't understand" ;; esacdoneecho echo "That's all folks!"
运行后有以下结果。
$ ./talk.shPlease talk to me ...helloHello yourself!What do you think of politics?Sorry, I don't understandbyeSee you again!That's all folks!$
继续学习变量。以下代码写入var3.sh
。
#!/bin/shecho "I was called with $# parameters"echo "My name is $0"echo "My first parameter is $1"echo "My second parameter is $2"echo "All parameters are $@"
运行结果如下:
$ /home/steve/var3.shI was called with 0 parametersMy name is /home/steve/var3.shMy first parameter isMy second parameter is All parameters are $$ ./var3.sh hello world earthI was called with 3 parametersMy name is ./var3.shMy first parameter is helloMy second parameter is worldAll parameters are hello world earth
继续学习变量。以下代码写入var4.sh
。
#!/bin/shwhile [ "$#" -gt "0" ]do echo "\$1 is $1" shiftdone
This script keeps on using shift
until $#
is down to zero, at which point the list is empty. Another special variable is $?
. This contains the exit value of the last run command. So the code:
#!/bin/sh/usr/local/bin/my-commandif [ "$?" -ne "0" ]; then echo "Sorry, we had a problem there!"fi
当进程退出时的代码为0
时,就没有问题。
#!/bin/shold_IFS="$IFS"IFS=:echo "Please input some data separated by colons ..."read x y zIFS=$old_IFSecho "x is $x y is $y z is $z"
写入var5.sh
。运行如下:
$ ./ifs.shPlease input some data separated by colons ...hello:how are you:todayx is hello y is how are you z is today
$ ./ifs.shPlease input some data separated by colons ...hello:how are you:today:my:friendx is hello y is how are you z is today:my:friend
使用默认变量。
#!/bin/shecho -en "What is your name [ `whoami` ] "read mynameif [ -z "$myname" ]; then myname=`whoami`fiecho "Your name is : $myname"
函数的用法。
#!/bin/sh# A simple script with a function...add_a_user(){ USER=$1 PASSWORD=$2 shift; shift; # Having shifted twice, the rest is now comments ... COMMENTS=$@ echo "Adding user $USER ..." echo useradd -c "$COMMENTS" $USER echo passwd $USER $PASSWORD echo "Added user $USER ($COMMENTS) with pass $PASSWORD"}#### Main body of script starts here###echo "Start of script..."add_a_user bob letmein Bob Holness the presenteradd_a_user fred badpassword Fred Durst the singeradd_a_user bilko worsepassword Sgt. Bilko the role modelecho "End of script..."
阶乘的shell实现,使用递归。
#!/bin/shfactorial(){ if [ "$1" -gt "1" ]; then i=`expr $1 - 1` j=`factorial $i` k=`expr $1 \* $j` echo $k else echo 1 fi}while :do echo "Enter a number:" read x factorial $xdone
模块化编程。以下代码写入common.lib
。
# common.lib# Note no #!/bin/sh as this should not spawn # an extra shell. It's not the end of the world # to have one, but clearer not to.#STD_MSG="About to rename some files..."rename(){ # expects to be called as: rename .txt .bak FROM=$1 TO=$2 for i in *$FROM do j=`basename $i $FROM` mv $i ${j}$TO done}
以下代码写入function2.sh
。
#!/bin/sh# function2.sh. ./common.libecho $STD_MSGrename .txt .bak
返回码。
#!/bin/shadduser(){ USER=$1 PASSWORD=$2 shift ; shift COMMENTS=$@ useradd -c "${COMMENTS}" $USER if [ "$?" -ne "0" ]; then echo "Useradd failed" return 1 fi passwd $USER $PASSWORD if [ "$?" -ne "0" ]; then echo "Setting password failed" return 2 fi echo "Added user $USER ($COMMENTS) with pass $PASSWORD"}## Main script starts hereadduser bob letmein Bob Holness from BlockbustersADDUSER_RETURN_CODE=$?if [ "$ADDUSER_RETURN_CODE" -eq "1" ]; then echo "Something went wrong with useradd"elif [ "$ADDUSER_RETURN_CODE" -eq "2" ]; then echo "Something went wrong with passwd"else echo "Bob Holness added to the system."fi
crontab的使用
在考虑向cron进程提交一个crontab文件之前,首先要做的一件事情就是设置环境变量EDITOR。cron进程根据它来确定使用哪个编辑器编辑crontab文件。9 9 %的UNIX和LINUX用户都使用vi,如果你也是这样,那么你就编辑$ HOME目录下的. profile文件,在其中加入这样一行:
EDITOR=vi; export EDITOR
然后保存并退出。不妨创建一个名为<user>cron
的文件,其中<user>
是用户名,例如,davecron
。在该文件中加入如下的内容。
# (put your own initials here)echo the date to the console every# 15minutes between 6pm and 6am0,15,30,45 18-06 * * * /bin/echo 'date' > /dev/console
保存并退出。确信前面5个域用空格分隔。
在上面的例子中,系统将每隔1 5分钟向控制台输出一次当前时间。如果系统崩溃或挂起,从最后所显示的时间就可以一眼看出系统是什么时间停止工作的。在有些系统中,用tty1来表示控制台,可以根据实际情况对上面的例子进行相应的修改。为了提交你刚刚创建的crontab文件,可以把这个新创建的文件作为cron
命令的参数:
$ crontab davecron
现在该文件已经提交给cron进程,它将每隔15分钟运行一次。
同时,新创建文件的一个副本已经被放在/var/spool/cron目录中,文件名就是用户名(即dave)。
为了列出crontab文件,可以用:
$ crontab -l