【学习笔记】《Linux命令行和shell脚本编程宝典》Richard Blum
第一个脚本文件
#!/bin/bashecho "This is my first bash code!"exit 0
重定向符号和数学计算
#!/bin/bashecho -n "The time and date are: "datevalue1=100 #等号前后不允许出现空格value2=$value1echo -n "value1="echo $value1echo -n "value2="echo $value2ls -l | sort > out.txt #管道符号(|)和重定向输出符号>ls -l >> out.txt #重定向追加输出符号>>echo -n "wc<out.txt:"wc < out.txt #重定向输入符号<echo "sort<<EOF ... EOF"sort << EOF #内置输入重定向<<`date`EOF#数学计算echo -n "expr进行计算:1+5="expr 1+5echo -n "使用方括号进行计算:1+5="echo $[1+5]echo "使用bc计算器进行浮点运算"var1=100var2=200var3=`echo "scale=4;$var1/$var2" | bc`echo "$var1 / $var2 = $var3"var4=71var5=`bc<<EOFscale=4a1=($var1*$var2)b1=($var3*$var4)a1+b1EOF`echo "var5=$var5"exit 0
使用test命令
#!/bin/bash#使用test命令var1=10var2=100if [ $var1 -gt $var2 ]then echo "var1 grate var2"else echo "var2 grate var1"fi#只能比较整数test_user=hanxiif [ $USER = $test_user ]then echo "Welcome $test_user"fistr1=Hanxistr2=hanxiif [ $str1 \> $str2 ]then echo "$str1 > $str2"else echo "$str1 < $str2"fiif [ -n $str1 ]then echo "The string '$str1' is not empty"else echo "the string '$str1' is empty"fi#检查文件目录if [ -d $HOME ]then echo "your Home dir exists" cd $HOME ls -aelse echo "there's a problem with your HOME dir"fipwfile=/etc/shadowif [ -f $pwfile ]then if [ -r $pwfile ] then tail $pwfile else echo "Sorry, I'm unable to reas the $pwfile file " fielse echo "Sorry, the file $pwfile doesn't exist"fiif [[ $USER == h* ]]then echo "Hello $USER"else echo "Sorry, I don't know you"fi
循环语句
#!/bin/bashfor file in /home/hanxi/*do if [ -d "$file" ] then echo "$file is a directory" elif [ -f "$file" ] then echo "$file is a file" fidonevar1=10while [ $var1 -gt 0 ]do echo $var1 var1=$[ $var1 - 1 ]donevar1=100until [ $var1 -eq 0 ]do echo $var1 var1=$[ $var1 - 25 ]done#文件数据的循环IFSOLD=$IFSIFS=$'\n'for entry in `cat /etc/passwd`do echo "Values in $entry -" IFS=: for value in $entry do echo " $value" donedone | morefor file in /home/hanxi/*do if [ -d "$file" ] then echo "$file is directory" elif echo "$file is a file" fidone > output.txt
读取参数
#!/bin/bashname=`basename $0`echo the commane entered is : $namec_args=$#echo count args:$c_args#取最后一个参数echo the last parameter is ${!#}echo all parameter: $*echo all parameter: $@count=1for param in "$@"do echo "\$@ parameter #$count = $param" count=$[ $count + 1 ]done#getoptswhile getopts :ab:c optdo case "$opt" in a) echo "Found the -a option";; b) echo "Found the -b option, with value $OPTARG";; c) echo "Found the -c option";; *) echo "Unknown option : $opt";; esacdoneshift $[ $OPTIND - 1 ]count=1for param in "$@"do echo "Parameter $count: $param" count=$[ $count + 1 ]doneread -p "Please enter your age:" ageecho age:$ageif read -t 5 -p "Please enter your name: " namethen echo "Hellp $name,welcome to my script"else echo echo "sorry ,too slow!"firead -n1 -p "Do you want to continue [Y/N]?" answercase $answer inY | y) echo echo " fine, continue on...";;N | n) echo echo OK,Good bye exit;;esacecho "This is the end of the script"read -s -p "Enter your password: " passechoecho "Is your password really $pass?"#读取文件count=1cat for.txt | while read linedo echo "Line $count: $line" count=$[ $count+1 ]doneecho "Finished processing the file"
重定向文件描述符
#!/bin/bash#永久重定向exec 9>&2exec 2>testerrorecho "this will in testerror">&2exec 2<&9exec 9<&0exec 0<testincount=1while read linedo echo "Line #$count:$line" count=$[ $count + 1 ]doneexec 0<&9#重定向文件描述符exec 3>&1exec 1>testoutecho "this should store in the output file"echo "along with this line."exec 1>&3echo "Now things should be back to nomarl"exec 4<&0exec 0<testincount=1while read linedo echo "Line #$count:$line" count=$[ $count + 1 ]doneexec 0<&4read -p "Are you done now?" answercase $answer inY|y) echo "Goodbye";;N|n) echo "continue...";esac#创建读写文件描述符exec 8<> testfileread line <&8echo "Read:$line"echo "This is a test line" >&8#关闭文件描述符exec 8>&-#列出文件描述服#`/usr/sbin/lsof -a -p $$`|more#禁止命令输出#2 > /dev/null#创建本地临时文件tempfile=`mktemp test.XXXXXX`exec 4>$tempfileecho "This is the first line">&3exec 4>&-#在/temp中创建临时文件tmpfile=`mktemp -t tmp.XXXXXX`echo "The temp file is located at:$tempfile"cat $tempfilerm -f $tempfile#创建临时文件夹tmpdir=`mktemp -d dir.XXXXXX`cd $tmpdirtempfile1=`mktemp temp.XXXXXX`ls -lcd ..#记录消息a=`date | tee testfile;\cat testfile;\date | tee -a testfile;\cat testfile`
信号处理
#!/bin/bash#信号处理trap "echo 'get a sign'" SIGINT SIGTERMtrap "echo byebye" EXITecho "This is a test program"count=1while [ $count -le 10 ]do echo "Loop #$count" sleep 10 count=$[ $count+1 ]doneecho "This is the end of the test program"trap - EXIT#移除捕获#后台牧师运行#./test6.sh &#不使用终端的情况下运行脚本#nohup ./test6.sh &#查看作业#jobs#重新启动作业#bg 2(作业序号)//后台#fg 2//前台#优先级#nice -n 10 ./test6.sh#renice 10 -p 25904(进程号)#预计时间运行at命令#at -f test6.sh 20:00#batch命令,系统平均负载低于0.8时运行,可以设定时间,比at命令更好#corn表格可以设定循环运行,格式:#min hour dayofmonth month dayofweek command#每个月第一天运行:#12 16 * * 1 command#每个月最后一天运行:#12 16 * * * if [ `date +%d =d tommorrow` = 01 ] ; then ; command
函数的使用
#!/bin/bash#函数#使用返回值function func1{ read -p "Enter a value: " value echo $[ $value * 2 ]}result=`func1`echo "the new value is $result"#传递参数function func2{ echo $[ $1+$2 ]}result=`func2 2 2`echo "the new result is $result"#局部变量, 递归function func3{ if [ $1 -eq 1 ] then echo 1 else local temp=$[ $1-1 ] local result=`func3 $temp` echo $[ $result*$1 ] fi}read -p "Enter value:" valueresult=`func3 $value`echo "the factorial of $value is: $result"#调用当前目录下到函数库#. ./myfuncs
TAG: