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

Linux-shell基础-2

2015-06-22 00:50 477 查看
各种引号的用法总结如下

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 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

read -p "input one of ont, two, three:" valu

case $valu in
"one")
echo "first"
;;
"two")
echo "second"
;;
"three")
echo "third"
;;
*)
echo "error"
;;
esac


函数声明必须在shell的最前面

function funName{

}

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

function prifxit(){
echo -n "you choice is: "
}
read -p "input one of ont, two, three:" valu

case $valu in
"one")
prifxit; echo "first"
;;
"two")
prifxit; echo "second"
;;
"three")
prifxit; echo "third"
;;
*)
prifxit; echo "error" | tr 'a-z' 'A-Z'   #特别注意大小写的转换
;;
esac


特别注意:sh sh17.sh不能运行,提示错误。这时候修改文件为可执行,就没有错误了。(虽然不知道为什么)
cp操作会复制文件的权限

fuhui@ubuntu:~/script$ chmod +x sh17.sh
fuhui@ubuntu:~/script$ ./sh17.sh
input one of ont, two, three:one
you choice is: first
fuhui@ubuntu:~/script$ ./sh17.sh
input one of ont, two, three:ss
you choice is: ERROR


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

function prifxit(){
echo -n "the function name $0 : $1"
}
read -p "input one of ont, two, three:" valu

case $valu in
"one")
prifxit "fuhui"; echo "first"
;;
"two")
prifxit "hui"; echo "second"
;;
"three")
prifxit "do"; echo "third"
;;
*)
prifxit "it"; echo "error" | tr 'a-z' 'A-Z'
;;
esac

函数的例子,函数内的$0并没有指明这个函数的名字,而是执行脚本的名称。
注意:函数的$1代表函数的第一个参数,依次类推
fuhui@ubuntu:~/script$ ./sh18.sh
input one of ont, two, three:
the function name ./sh18.sh : itERROR
fuhui@ubuntu:~/script$ ./sh18.sh
input one of ont, two, three:one
the function name ./sh18.sh : fuhuifirst


while [ condition ] <==中括号内为判断式
do <==do 循环的开始!
程序段落
done <==done 循环的结束

until [ condition ]
do
程序段落
done

两个循环中值得条件不一样,正好相反。


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

while [ "$yn" != "yes" -a "$yn" != "YES" ]       #特别注意-a是&&的意思,-o是||的意思 ,支持 !=
do
read -p "please input a option :" yn
done
echo "you option is right"


until的用法,注意准换大小写的用法

V=hello
V=`echo $V | tr '[:lower:]' '[:upper:]'`
echo $V

下面的转化效果是一样的
echo as | tr '[:lower:]' '[:upper:]'
echo as | tr [:lower:] [:upper:]


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

until [ "$yn" = "YES" ]
do
read -p "please input a option :" yn
yn=$(echo $yn | tr 'a-z' 'A-Z')
if [ "$yn" = "EXIT" ]; then
echo "exit program"
break;
fi
done


计算1+2+。。。+100

declare -i num=0
done
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

declare -i sum=0
declare -i num=0

while [ $num -le 100 ]		#特别注意 -le 平时使用的时候都是 test $num -le 100
do
sum=$(($sum+$num))  #需要注意$sum 和sum的区别
num=$(($num+1))
done
echo  "the sum is: $sum"
exit 0
~
echo "you option is right"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell while case until function