shell基础2(一)

2015-07-24 06:56:10 · 作者: · 浏览: 8

各种引号的用法总结如下

1、 单引号 ‘

由单引号括起来的字符都作为普通字符出现。特殊字符用单引号括起来以后,也会失去原有意义,而只作为普通字符解释。

2、 双引号 “

由双引号括起来的字符,除 、\、′、和”这几个字符仍是特殊字符并保留其特殊功能外,其余字符仍作为普通字符对待。对于 来说,就是用其后指定的变量的值来 代替这个变量和 ;对于而言,是转义字符,它告诉shell不要对其后面的那个字符进行特殊处理,只当作普通字符即可。可以想见,在双引号中需要在前面加上的只有四个字符 ,,’和”本身。而对”号,若其前面没有加,则Shell会将它同前一个”号匹配。

3、 反引号 `

反引号(`)这个字符所对应的键一般位于键盘的左上角,不要将其同单引号(’)混淆。反引号括起来的字符串被shell解释为命令行,在执行时,shell首先执行该命令行,并以它的标准输出结果取代整个反引号(包括两个反引号)部分。


#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

if [ "$1" = "hello" ]; then
        echo "hello, who are you?"
elif [ "$1" = "" ]; then
        echo "you must input parameters,ex>{$0 somewprd}"
else
        echo  "the only parameter is 'hello',ex>{$0 hello}"
fi
~

可以进行空字符串的比较 == "",这里的{} 只是一个字符

netstat -tuln
127.0.0.1 表示仅对本机开放
0.0.0.0 或者:::: 表示对整个internet开放


不能运行,尝试的例子 #!/bin/bash # program:test example 01 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin export PATH netstat_list=$(netstat -tuln) if [ $($netstat%":80") ]; then echo ":80" fi 
#!/bin/bash # program:test example 01 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin export PATH testing=$(netstat -tuln | grep ":80") #$() 命令行之前没有空格 if [ "$testing" != "" ]; then echo "www is running in your system" fi testing=$( netstat -tuln | grep ":22" ) #$() 命令行之前可以有空格 if [ "$testing" != "" ]; then echo " ssh is running in your system" fi ~

#!/bin/bash # program:test example 01 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin export PATH read -p "please input your time:" lasttime nowtime=$( date +%s ) lasttime=$( date --date "$lasttime" +%s ) echo $[[$lasttime-$nowtime ]] exit 0 为什么输出来是个这了,没有进行减法操作(当作了字符串???还是(()) ) fuhui@ubuntu:~/script$ sh sh13.sh please input your time:20150902 $[[1441177200-1434859352 ]]

正则匹配的测试 fuhui@ubuntu:~/script$ echo 1234 | grep "[0-9]\{3\}" 1234 在{}需要进行转码,没有//这样的标志 

1 declare -i number
2 # 脚本余下的部分会把”number”当作整数看待.


 #!/bin/bash # program:test example 01 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin export PATH read -p "please input your time: ex(20100101)" lasttime date_d=$( echo $lasttime | grep '[0-9]\{8\}') #重点学习一下正则的匹配 if [ "$date_d" = "" ]; then echo "you input wrong date format.." exit 1 fi declare -i date_dem=`date --date="$date_d" +%s` declare -i date_new=$( date +%s ) declare -i date_total=$(($date_dem-$date_new)) declare -i date_day=$(($date_total/60/60/24)) if [ $date_day -lt 0 ]; then echo "you had been demobilization before $((-1*$date_day))" fi ~ 

出现了一个错误,如下所示:
fuhui@ubuntu:~/script$ sh sh13.sh
please input your time: ex(20100101)20110505
sh13.sh: 13: sh13.sh: declare: not found
sh13.sh: 14: sh13.sh: declare: not found

修改了文件属性如下,错误就没有了
chmod +x sh13.sh


case语句

case $变量名称 in <==关键词为 case ,相当于switch的变量
“第一个变量内容”) <==每个变量内容建议用双引号括起来,关键词则为小括号 )
程序段
;; <==操作结尾使用两个连续的分号来处理!
“第二个发量内容”)
程序段
;;
) <==最后一个变量内容都会用 来代表所有其他值
类似于default语句,结尾有;;符
;;
esac <==最终的 case 结尾,case的反向书写形式


#!/bin/bash # program:test example 01 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin export PATH case $1 in "hello") echo "continue to do " ;; "") echo "you should input a option" ;; *) echo "you must input parameter,ex >$0,someoption" ;; esac fuhui@ubuntu:~/script$ sh sh15.sh 3 you must input parameter,ex >sh15.sh,someoption fuhui@ubuntu:~/script$ sh sh15.sh hello #输入"hello"和hello居然是一样的 continue to do fuhui@ubuntu:~/script$ sh sh15.sh "hello" continue to do 

#!/bin/bash # program:test example