1 #!/bin/bash
2 num=`curl -s http://test.user.www.com/userinfo/`
3 echo $num
4 for (( i=1; i<$num; i++))
5 do
6 echo "$i"
7 done
8
9 i=0
10 while [ $i -lt $num ]
11 do
12 echo $i
13 i=`expr $i + 1`
14 done
#!/bin/bash
if [ ! -n "$1" ] ;then
echo "参数不能为空";
exit;
fi
[root@web_test test]# cat 1.sh
#!/bin/bash
check=`ps aux|grep nginx|grep -v grep`
if [ $? -eq 0 ];
then
echo "nginx ok";
exit;
else
echo "nginx down"
/usr/bin/nginx
fi
[root@web_test test]# cat 11.sh
result=`ssh root@localhost -p22 "/bin/bash /home/test/1.sh"`
if [ "$result" == "nginx ok" ];then
echo "ok";
exit;
else
echo "error";
fi
[root@web_test test]#
判断两个字符串是否相等
cat if.sh
1 #!/bin/bash
2 #who=$1
3 #echo $who
4 a="book food";
5 b="food";
6 #if test $a = $b
7 if [ "$a" = "$b" ]; then
8 echo 'they are the same';
9 else
10 echo "no"
11 fi
变量 含义
$0 脚本名字
$1 位置参数 #1
$2 - $9 位置参数 #2 - #9
${10} 位置参数 #10
$# 位置参数的个数
"$*" 所有的位置参数(作为单个字符串) *
"$@" 所有的位置参数(每个都作为独立的字符串)
${#*} 传递到脚本中的命令行参数的个数
${#@} 传递到脚本中的命令行参数的个数
$? 返回值,显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。
$$ 脚本的进程ID(PID)
$- 传递到脚本中的标志(使用set),显示shell使用的当前选项,与set命令功能相同
$_ 之前命令的最后一个参数
$! 运行在后台的最后一个作业的进程ID(PID)
* 必须被引用起来, 否则默认为"$@".
二元比较
操作 描述 ----- 操作 描述
算术比较 字符串比较
-eq 等于 = 等于
== 等于
-ne 不等于 != 不等于
-lt 小于 \< 小于 (ASCII) *
-le 小于等于
-gt 大于 \> 大于 (ASCII) *
-ge 大于等于
-z 字符串为空
-n 字符串不为空
算术比较 双括号(( ... ))结构
> 大于
>= 大于等于
< 小于
<= 小于等于
* 如果在双中括号 [[ ... ]] 测试结构中使用的话, 那么就不需要使用转义符\了.
文件类型的测试操作
操作 测试条件 ----- 操作 测试条件
-e 文件是否存在 -s 文件大小不为0
-f 是一个标准文件
-d 是一个目录 -r 文件具有读权限
-h 文件是一个符号链接 -w 文件具有写权限
-L 文件是一个符号链接 -x 文件具有执行权限
-b 文件是一个块设备
-c 文件是一个字符设备 -g 设置了sgid标记
-p 文件是一个管道 -u 设置了suid标记
-S 文件是一个socket -k 设置了"粘贴位"
-t 文件与一个终端相关联
-N 从这个文件最后一次被读取之后, 它被修改过 F1 -nt F2 文件F1比文件F2新 *
-O 这个文件的宿主是你 F1 -ot F2 文件F1比文件F2旧 *
-G 文件的组id与你所属的组相同 F1 -ef F2 文件F1和文件F2都是同一个文件的硬链接 *
! "非" (反转上边的测试结果)
* 二元操作符(需要两个操作数).
参数替换和扩展
表达式 含义
${var} 变量var的值, 与$var相同
${var-DEFAULT} 如果var没有被声明, 那么就以$DEFAULT作为其值 *
${var:-DEFAULT} 如果var没有被声明, 或者其值为空, 那么就以$DEFAULT作为其值 *
${var=DEFAULT} 如果var没有被声明, 那么就以$DEFAULT作为其值 *
${var:=DEFAULT} 如果var没有被声明, 或者其值为空, 那么就以$DEFAULT作为其值 *
${var+OTHER} 如果var声明了, 那么其值就是$OTHER, 否则就为null字符串
${var:+OTHER} 如果var被设置了, 那么其值就是$OTHER, 否则就为null字符串
${var?ERR_MSG} 如果var没被声明, 那么就打印$ERR_MSG *
${var:?ERR_MSG} 如果var没被设置, 那么就打印$ERR_MSG *
${!varprefix*} 匹配之前所有以varprefix开头进行声明的变量
${!varprefix@} 匹配之前所有以varprefix开头进行声明的变量
* 当然, 如果变量var已经被设置的话, 那么其值就是$var.
字符串操作
表达式 含义
${#string} $string的长度
${string:position} 在$string中, 从位置$position开始提取子串
${string:position:length} 在$string中, 从位置$position开始提取长度为$length的子串
${string#substring} 从变量$string的开头, 删除最短匹配$substring的子串
${string##substring} 从变量$string的开头, 删除最长匹配$substring的子串
${string%substring} 从变量$string的结尾, 删除最短匹配$substring的子串
${string%%substring} 从变量$string的结尾, 删除最长匹配$substring的子串
${string/substring/replacement} 使用$replacement, 来代替第一个匹配的$substring
${string//substring/replacement} 使用$replacement, 代替所有匹配的$substring
${string/#substring/replacement} 如果$string的前缀匹配$substring, 那么就用$replacement来代替匹配到的$substring
${string/%substring/replacement} 如果$string的后缀匹配$substring, 那么就用$replacement来代替匹配到的$substring
expr match "$string" '$substring' 匹配$string开头的$substring*的长度
expr "$string" : '$substring' 匹配$string开头的$substring*的长度
expr index "$string" $substring 在$string中匹配到的$substring的第一个字符出现的位置
expr substr $string $position $length 在$string中从位置$position开始提取长度为$length的子串
expr match "$string" '\($substring\)' 从$string的开头位置提取$substring*
expr "$string" : '\($substring\)' 从$string的开头位置提取$substring*
expr match "$string" '.*\($substring\)' 从$string的结尾提取$substring*
expr "$string" : '.*\($substring\)' 从$string的结尾提取$substring*
* $substring是一个正则表达式.
一些结构的汇总
表达式 解释
中括号
if [ CONDITION ] 测试结构
if [[ CONDITION ]] 扩展的测试结构
Array[1]=element1 数组初始化
[a-z] 正则表达式的字符范围
大括号
${variable} 参数替换
${!variable} 间接变量引用
{ command1; command2; . . . commandN; } 代码块
{string1,string2,string3,...} 大括号扩展
圆括号
( command1; command2 ) 子shell中执行的命令组
Array=(element1 element2 element3) 数组初始化
result=$(COMMAND) 在子shell中执行命令, 并将结果赋值给变量
>(COMMAND) 进程替换
<(COMMAND) 进程替换
双圆括号
(( var = 78 )) 整型运算
var=$(( 20 + 5 )) 整型运算, 并将结果赋值给变量
引号
"$variable" "弱"引用
'string' "强"引用
后置引用
result=`COMMAND` 在子shell中运行命令, 并将结果赋值给变量
cat test.sh
#!/bin/bash
TEST=3
echo "$TEST = 3"
echo '$TEST = 3'
root@raspberrypi:/home/pi/test# ./1.sh
3 = 3
$TEST = 3
cat read.sh
#!/bin/sh
# read
read First Second Third
echo "The First is :" $First
echo "The Second is :" $Second
echo "The Third is :" $Third
root@raspberrypi:/home/pi/test# ./1.sh
1 2 3
The First is : 1
The Second is : 2
The Third is : 3
1 #!/bin/bash
2 while (( 1 != 0 ))
3 do
4 #i=`ifconfig |grep 10.77|awk -F . '{print $2}'`
5 #echo $i
6 #if [ -e 1.txt ]; then echo "file"; else echo "no"; fi
7 if [ -e 1.txt ]
8 then
9 let i=i+1
10 #/usr/bin/sshfs orangepi@192.168.1.115:/mnt/sdaPi /mnt/sdaPi -o allow_other,nonempty
11 echo $i
12 sleep 3
13 continue
14 else
15 /bin/touch /root/1.txt
16 echo "no";
17 sleep 3
18 fi
19 done
相等eq,不相等ne
if条件语名
if条件判断
if多条件判断
tm=$(date +%H%M)
echo $tm
if [[ $tm -gt 800 && $tm -lt 2100 ]];
then
echo "ok";
else
#exit;
echo "no";
fi
[test@06_09 ~]$ echo $UID
500
[test@06_09 ~]$ if [ $UID -gt 0 ]
> then
> echo "test"
> fi
test
[test@06_09 ~]$ if [ $UID -gt 0 ]; then echo "test"; fi
#!/bin/bash
file="./file"
if [ -e $file ]; then
echo "File exists"
else
echo "File does not exists"
fi
判断变量
read -p "input a word :" word
if [ ! -n "$word" ] ;then
echo "you have not input a word!"
else
echo "the word you input is $word"
fi
判断输入参数
#!/bin/bash
if [ ! -n "$1" ] ;then
echo "you have not input a word!"
else
echo "the word you input is $1"
fi
以下未验证,转自http://blog.csdn.net/lllxy/article/details/3255554
2. 直接通过变量判断
如下所示:得到的结果为: IS NULL
#!/bin/sh
para1=
if [ ! $para1 ]; then
echo "IS NULL"
else
echo "NOT NULL"
fi
3. 使用test判断
得到的结果就是: dmin is not set!
#!/bin/sh
dmin=
if test -z "$dmin"
then
echo "dmin is not set!"
else
echo "dmin is set !"
fi
4. 使用""判断
#!/bin/sh
dmin=
if [ "$dmin" = "" ]
then
echo "dmin is not set!"
else
echo "dmin is set !"
fi
if [ $first = 'hello' ]
then
echo "right"
else
echo "error"
fi
if test $first='hello'
then
echo "111111111111111";
fi
content=`ls`
echo $content
who=`who`
echo $who
echo $who >> 1.result
whoami=`whoami`
result=`grep -c $whoami 1.result`
echo "result:" $result;
if [ $result -gt 0 ]
then
echo "找到内容"
else
echo "木牛"
fi
if who | grep 'test115'
then
echo "yes"
else
echo "no"
fi
mount.sh
1 who=$1
2 #echo $who
3 if test $who = 'company'
4 then
5 echo "公司ip";
6 ip="192.168.99.249";
7 else
8 echo "小屋子ip";
9 ip="192.168.2.112";
10 fi
11
12 echo $ip
13 mount //$ip/virtual_xp /mnt/virtual_xp
14 echo "mount xp success"
#!/bin/bash
# Declare variable choice and assign value 4
choice=4
# Print to stdout
echo "1. Bash"
echo "2. Scripting"
echo "3. Tutorial"
echo -n "Please choose a word [1,2 or 3]? "
# Loop while the variable choice is equal 4
# bash while loop
while [ $choice -eq 4 ]; do
# read user input
read choice
# bash nested if/else
if [ $choice -eq 1 ] ; then
echo "You have chosen word: Bash"
else
if [ $choice -eq 2 ] ; then
echo "You have chosen word: Scripting"
else
if [ $choice -eq 3 ] ; then
echo "You have chosen word: Tutorial"
else
echo "Please make a choice between 1-3 !"
echo "1. Bash"
echo "2. Scripting"
echo "3. Tutorial"
echo -n "Please choose a word [1,2 or 3]? "
choice=4
fi
fi
fi
done
for循环
1003 if [ -f 1.txt ]; then echo "file"; else echo "no"; fi
1004 if [ -f 1.txt1 ]; then echo "file"; else echo "no"; fi
1005 if [ -f 1.txt ]; then echo "file"; else echo "no"; fi
1006 if [ -d 1.txt ]; then echo "file"; else echo "no"; fi
1007 if [ -e 1.txt ]; then echo "file"; else echo "no"; fi
1008 if [ -r 1.txt ]; then echo "file"; else echo "no"; fi
1009 ll
1010 if [ -x 1.txt ]; then echo "file"; else echo "no"; fi
1011 if [ -x 1.txt ]; then echo "file"; else echo "no"; fi
1012 if [ -z 1.txt ]; then echo "file"; else echo "no"; fi
1013 if [ -n 1.txt ]; then echo "file"; else echo "no"; fi
1014 num=10
1015 if [ $num -gt 5 ]; then echo "file"; else echo "no"; fi
1016 if [ num -gt 5 ]; then echo "file"; else echo "no"; fi
1017 if [ $num -gt 5 ]; then echo "file"; else echo "no"; fi
1018 if [ $num -gt 115 ]; then echo "file"; else echo "no"; fi
1022 test 1=1 && echo "ok"
1023 test 1<2 && echo "ok"
1024 test 1 -lt 2 && echo "ok"
1025 test 1 -gt 2 && echo "ok"
1028 for m in {1..100}; do test -d .;done;
1029 for m in {1..100}; do; test -d .;done;
1030 for m in {1..100}; do test -d .; done
1031 for m in {1..100}; do test -d $m; done
1032 for m in {1..100000}; do test -d $m; done
1033 b=$((5*5+5-3/2))
1034 echo $b
1035 $((1*2))
1036 b=$((1*2))
1037 echo $b
1038 $((1*2))
1039 b=$((1*2))
1040 echo $b
1041 b=$((1+0.2))
1042 b=$((1*2))
1043 echo $b
1044 echo $(pwd)
1045 `pwd`
1046 pwd=`pwd`
1047 echo $pwd
1048 c=$("0.1+0.2"|bc)
1049 c=$(echo "0.1+0.2"|bc)
1050 echo $c
1051 b=$((1+2*3))
1052 echo $b
1053 b=expr 1 + 2*3
1054 b=expr 1 + 2\*3
1055 b=expr 1 + 2
1056 b= expr 1 + 2
1057 b= expr 1 + 2*3
1058 b= expr 1 + 2*3
1059 b= expr 1 + 2\*3
1060 b= expr 1 + 6
1061 pwd
1062 expr 1 +2
1063 expr 1 + 2
1064 a=`expr 1 + 2`
1065 echo $a
1066 a=`expr 1 + 2*3`
1067 a=`expr 1 + 2\*3`
1068 a=`expr 1 + 2 \* 3`
1069 echo $a
28、循环
[test@06_09 ~]$ for i in {1..10}; do echo $i; done
1
2
3
4
5
6
7
8
9
10
[test@06_09 ~]$ for a in {a..z}; do echo $a; done
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
test@test:~/桌面$ j=0;for i in {5..10}; do let j++; echo $j"、"$i; done;
1、5
2、6
3、7
4、8
5、9
6、10
1 #! /bin/sh
2 content=`grep -n 'go*d' ./grep.txt`
3 for i in $content
4 do
5 echo "$i"
6 done
for.sh
for((i=1;i<=10;i++));do
echo $i;
done;
ls.sh
2013-01-14 19:58:59| 分类: linux | 标签:ls.sh grep filename类型过滤 字号
#!/bin/sh
list=`ls`
#echo $ls
for i in $list
do
#echo $i
#echo $i
count=`echo $i | grep -cP ".*html"`
#echo $count;
#echo $?
#exit
if [ $count -gt 0 ];
then
echo $i
fi
done
#!/bin/bash
echo "共"$#"参数"
echo "脚本名称"$0
echo "所有的参数"$*
echo "参数1"$1
echo "参数2"$2
#循环显示所有的参数
for i in $*
do
echo $i
done
#变量b是否在变量a中
a=world
b=lld
echo $a | grep $b
#echo $?
#echo $? #0找到,>0没有找到
if [ $? -gt 0 ];
then
echo "没有找到"
else
echo "找到"
fi
i=0;
echo $i;
let i=i+1
echo $i
cd /home
echo $(pwd)
ls=$(ls)
#echo $ls
for i in $ls
do
echo $i | grep 'tar.gz'
echo "result:"$?
# then
# echo "yes"
# fi
done
#!bin/bash
for tm in "moring" "nonn" "evening"
do
echo $tm
done
for loop in a b c d e f
do
echo $loop
sleep 1
done
echo "end"
a.txt
a
b
c
for loop in `cat a.txt`
do
echo $loop
done
echo "end"
for((i=0;i<10;i++)
do
echo $i
sleep 1
done
echo "End"
#!/bin/bash
#a.sh
for loop in $*
do
echo $loop
sleep 1
done
chmod 755 a.sh
./a.sh
process.sh
#!/bin/bash
echo "共"$#"参数"
echo "脚本名称"$0
echo "所有的参数"$*
echo "参数1"$1
echo "参数2"$2
#循环显示所有的参数
for i in $*
do
echo $i
done
#变量b是否在变量a中
a=world
b=lld
echo $a | grep $b
#echo $?
#echo $? #0找到,>0没有找到
if [ $? -gt 0 ];
then
echo "没有找到"
else
echo "找到"
fi
i=0;
echo $i;
let i=i+1
echo $i
cd /home
echo $(pwd)
ls=$(ls)
#echo $ls
for i in $ls
do
echo $i | grep 'tar.gz'
echo "result:"$?
# then
# echo "yes"
# fi
done
test@acer:~$ ./process.sh test 123456 28
共3参数
脚本名称./process.sh
所有的参数test 123456 28
参数1test
参数2123456
test
123456
28
没有找到
0
1
/home
result:1
test.tar.gz
result:0
1 #!/bin/bash
2 case $1 in
3 *.jpg) gqview $1;;
4 *.txt) vim $1;;
5 *.avi | .wmv) mplayer $1;;
6 *) echo "$1:don't know how to read this file";;
7 esac
# vi case.sh
echo $@
echo $0
echo $$
echo $*
echo $#
read -p "pls input a num: " num
echo "the input data is $num"
case $num in
1) echo "January";;
2) echo "Feburary";;
5) echo "may";;
*) echo "not correct input";;
esac
#!/bin/bash
echo "What is your preferred programming / scripting language"
echo "1) bash"
echo "2) perl"
echo "3) phyton"
echo "4) c++"
echo "5) I do not know !"
read case;
#simple case bash structure
# note in this case $case is variable and does not have to
# be named case this is just an example
case $case in
1) echo "You selected bash";;
2) echo "You selected perl";;
3) echo "You selected phyton";;
4) echo "You selected c++";;
5) exit
esac
shell编程——case语句
(2009-08-06 23:31:13)
转载▼
标签:
case
)
命令行参数
杂谈
分类: python-perl-shell脚本编程
case语句格式
# vi test.sh
:
echo "input : "
read num
echo "the input data is $num"
case $num in
1) echo "January";; 双分号结束
2) echo "Feburary";;
5) echo "may" 每个case可以有多条命令
echo "sdfd"
echo "sdf";; 但最后一条命令一定是双分号结束
*) echo "not correct input";; *)是其他值、default的意思
esac
# sh ./test.sh
input :
2
the input data is 2
Feburary
# sh ./test.sh
input :
ter
the input data is ter
not correct input
case 语句如果某个选项没有任何语句,也要加;; 否则会出下边错误
test: line 166: syntax error near unexpected token `)'
test: line 166: `"system hostname config")'
为什么输入no,仍不匹配到[no]
原来[]是专门针对单字符的值,如果用[no],就是n和o之一
case $yn in
[no]) return 1;;
* ) echo "only accept Y,y,N,n,YES,yes,NO,no" >&2;;
[macg@mac-home ~]$ sh test.sh
enter y/n :
no
only accept Y,y,N,n,YES,yes,NO,no
改正
case $yn in
no) return 1;;
NO) return 1;;
* ) echo "only accept Y,y,N,n,YES,yes,NO,no" >&2;;
esac
[macg@mac-home ~]$ sh test.sh
enter y/n :
no
一个getyn()函数的例子
getyn( )
{
while echo "enter y/n :"
do
read yn
case $yn in
[Yy]) return 0 ;;
yes) return 0 ;;
YES) return 0 ;;
[Nn]) return 1 ;;
no) return 1;;
NO) return 1;;
* ) echo "only accept Y,y,N,n,YES,yes,NO,no" ;;
esac
done
}
if
getyn 调用函数 以函数作为if条件,必须用if command法
then 注意0为真
echo " your answer is yes"
else
echo "your anser is no"
fi
if, case,匹配字符串最常见,但如何匹配一段很长的输出,一堆文字?最好方法,用“*”,如:*"command not found"*
[macg@machome ~]$ vi test.sh
var=$(ls -l $1) $()取命令输出,$1是命令行参数
echo "output is $var"
case $var in
"-rw-rw-r--"*) echo "this is not a execute file";;
"-rwxrwxr-x"*) echo "this is a execute file";
注意*在双引号外边
esac
[macg@machome ~]$ sh test.sh 22.txt
output is -rw-rw-r-- 1 macg macg 15 Jun 9 19:00 22.txt
this is not a execute file
[macg@machome ~]$ chmod +x 22.txt
[macg@machome ~]$ sh test.sh 22.txt
output is -rwxrwxr-x 1 macg macg 15 Jun 9 19:00 22.txt
this is a execute file
例2.匹配file命令输出的一堆文字,以获知文件类型
用’ ’ 取输出,然后用CASE+*对输出做修饰处理.
[macg@machome ~]$ vi test.sh
var=`file $1` ` `和$( )作用相同,是取命令输出
echo "output is $var"
case $var in
"$1: ASCII text"*) echo "this is a text file";;
"$1: directory"*) echo "this is a directory";;
注意*在双引号外边
esac
[macg@machome ~]$ sh test.sh 22.txt
output is 22.txt: ASCII text
this is a text file
[macg@machome ~]$ sh test.sh test-dir
output is test-dir: directory
this is a directory
最典型的shell case命令匹配命令行,用于sys v启动脚本的start|stop|restart|status处理
case "$@" in
($@ 字符串数组:以"参数1" "参数2" ... 的字符串数组形式保存所有参数
对于单个参数的情况,$@就是一个字符串)
start)
echo -n "Starting firewall..."
。。。
echo "OK!"
exit 0
;;
stop)
echo -n "Stopping firewall..."
。。。
exit 0
;;
restart)
$0 stop $0即执行原始程序
$0 start
;;
status)
clear
echo ">------------------------------------------"
iptables -L
echo ">------------------------------------------"
iptables -t nat -L POSTROUTING
exit 0
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
#!/bin/bash
COUNT=6
# bash while loop
while [ $COUNT -gt 0 ]; do
echo Value of count is: $COUNT
let COUNT=COUNT-1
done
Bash while loop
while.sh
#!/bin/bash
i=0
while [ $i -lt 10 ]
do
echo $i
i=`expr $i + 1`
done
#!/bin/sh
result=0
i=0
while [ $i -le 10 ]
do
#result=$(($result+$i))
let result=result+i
#i=$(($i + 1))
let i=i+1
#echo "result = $result; "
done
echo $result;
#!/bin/bash
sum=0
while [ $# -gt 0 ]; do
((sum += $1))
shift
done
echo "sum = $sum"
#http://linuxconfig.org/bash-scripting-tutorial
#!/bin/bash
PS3='Choose one word: '
# bash select
select word in "linux" "bash" "scripting" "tutorial"
do
echo "The word you have selected is: $word"
# Break, otherwise endless loop
break
done
exit 0
i=1
while [ $i -le 9 ]
do
row=1
colume=$i
while [ $row -le $i ]
do
if((i==9))
then
echo -n "$row+$colume=$((row+colume)) "
else
echo -n "$row+$colume=$((row+colume)) "
fi
row=$(expr $row + 1)
colume=$(expr $colume - 1)
done
echo
((i=$i+1))
done
#!/bin/bash
for ((i=1;i<=9;i++))
do
for ((j=1;j<=i;j++))
do
echo -n " $j*$i=$(($i*$j)) "
done
echo
done
11. Bash File Testing
-b filename Block special file
-c filename Special character file
-d directoryname Check for directory existence
-e filename Check for file existence
-f filename Check for regular file existence not a directory
-G filename Check if file exists and is owned by effective group ID.
-g filename true if file exists and is set-group-id.
-k filename Sticky bit
-L filename Symbolic link
-O filename True if file exists and is owned by the effective user id.
-r filename Check if file is a readable
-S filename Check if file is socket
-s filename Check if file is nonzero size
-u filename Check if file set-ser-id bit is set
-w filename Check if file is writable
-x filename Check if file is executable