-------------------------测试文件状态
------------------------测试文件a.log是否可用写0表示真1:
表示假
[-wa.log]或者test–wa.log
参数信息
-d目录-s文件长度大于0、非空
-f正规文件-w可写
-L符号连接-u文件有suid位设置
-r可读-x可执行
----------------------------逻辑运算-a表示与-o表示或
Test–wa.log–a–xa.logtest–wa.log–o–xa.log
Echo$?
可用打印出结果
-------------------------字符串比较
―――――――比较两个字符串是否相等
=两个字符串相等。
!
=两个字符串不等。
-z空串。
-n非空串。
test"aaa"="bbb"test"aaa"!
="bbb"test–z"$path"test–z"$path"
------------------------------数字比较
-eq数值相等。
-ne数值不相等。
-gt第一个数大于第二个数。
-lt第一个数小于第二个数。
-le第一个数小于等于第二个数。
-ge第一个数大于等于第二个数。
Test12–gl23
----------------------------expr命令一般用于整数值,但也可用于字符串。
一般格式为:
exprargumentoperatorargument
expr12+23
---------------------------vi命令替换,将test替换程jimmy
:
%s/test/jimmy/g
―――――――参数个数测试
echo"thisisparameterpractice!
!
!
!
"
if
[$#-lt3];
then
echo"theparameterislessthanthree";
exit1;
fi
echo"theparameteris:
"
echo"oneis$1"
echo"oneis$2"
echo"oneis$3"
――――――――――――文件可写脚本
RIGHT=/home/jimmy/ly.log
if[!
-w"$RIGHT"];
then
echo"youcannotwriteanythingtoly.log"
else
echo"youcanwriteanythingtoly.log$RIGHT"
fi
――――――――――――――――测试是否设置了环境变量
iftest-z$EDITOR
then
echo"theEDITERENVIRONMENTISNOTSET"
else
echo"theEDITERENVIRONMENTISSETED"
fi
――――――――――――――检查执行当前脚本的用户是否是root账户
if["$LOGNAME"!
="root"]
then
echo"thisordermustexcutebyroot,$LOGNAME">&2;
exit1;
else
echo"yes,youaretheroot">&1
exit1;
fi
―――――――――――――――――――――将参数传给系统命令
DIRECTORY=$1;
if
test-z"`ls-a$DIRECTORY`"
then
echo"the$DIRECTORYisEMPTY"
else
echo"the$DIRECTORYisnotempty"
fi
―――――――――按照输入的参数建立目录,如果目录存在则推出,如果目录不存在则提示用户是否建立,键入“y”则建立文件夹
DIRECTORY=$1
echo"test==============="
if["$DIRECTORY"=""]
then
echo"erroryoumustinputtheparameter,Basename$0">&2;
exit1;
fi
echo"$DIRECTORY======"
if[-d"$DIRECTORY"]
then
echo"the$DIRECTORYisexsit!
!
!
!
"
:
#donothing
else
echo"Thedirectorydoesnotexsit"
echo-n"Createitnow[Y...N]"
readANS
if["$ANS"="y"]||["$ANS"="Y"]
then
echo"Creatingnow";
mkdir$DIRECTORY>/home/jimmy/ly.log2>&1;
if
test$?
!
="0"
then
echo"createdir$DIRECTORYerror">&2;
exit1;
fi
else:
#donothing
fi
fi
――――――――提示用户是否要改变数据。
如果是则提示输入数字,然后将输入的数字相加,最后返回
COUNT=100
echo"doyouwanttochangethe$COUNT"
readtemp
if["$temp"="y"]||["$temp"="Y"]
then
echo"enterthevalue"
readvalue
expr$COUNT+100>./ly.log2>&1
Status=$?
if["$value"=""]||["$Status"!
="0"]
then
echo"thereissomeerror">./ly.log2>&1
exit1;
fi
COUNT=`expr$COUNT+$value`
echo"Nowthecountvalueis:
$COUNT"
fi
――――――输入用户和密码登陆脚本。
。
输入密码是不显示在屏幕上
IVALID_NAME=yes
IVALID_PASS=yes
#将终端信息保存在变量中
SAVEDSTTY=`stty-g`
echo"youwillloggingintosensitivearea:
"
echo-n"pleaseentertheusername:
"
readusername
#将终端的echo属性输入不显示在屏幕上
stty-echo
echo-n"enterthepassword:
"
readpassword
#将终端的所有属性还原
stty$SAVEDSTTY
if["$username"="lzw"-o"$username"="ly"]
then
IVALID_NAME=no
fi
if["$password"="lzw"]
then
IVALID_PASS=no
fi
echo"====$IVALID_NAME===$IVALID_PASS"
if["$[IVALID_NAME]"="yes"-o"$IVALID_PASS"="yes"]
then
if["$IVALID_NAME"="yes"]
then
echo"theusernameisundifined,youcannotloggingthisarea"
else
if["$IVALID_PASS"="yes"]
then
echo"thepasswordisnotright,youcannotloggingthisarea"
fi
fi
fi
―――――――――――――――――case语句,匹配到后立即跳出case语句
echo-n"pleaseenterthenumber:
"
readnum
case$numin
1)echo"youselected1:
"
;;
2)echo"youselected2:
"
;;
3)echo"youselected3:
"
;;
4)echo"youselected4:
"
;;
5)echo"youselected5:
"
;;
6)echo"youselected6:
"
;;
7)echo"youselected7:
"
;;
8|11)echo"youselected8:
"
;;
91)echo"youselected91:
"
;;
#匹配任意单字字符
?
)echo"youinputisnotnumberis:
$num"
;;
#匹配任意字符
*)echo"youselected*:
"
;;
Esac
――――――――――――――case语句参数传递
if[$#!
=1]
then
echo"theparametercountmustonlyone:
"
exit1;
fi
parm=$1;
case$parmin
start|START)echo"theprogrameisstarting...."
;;
stop|STOP)echo"theprogrameisstoping...."
;;
*)echo"theparameteriswrong...."
;;
Esac
――――――――缺省值,如果输入信息只是回车换行,那么就取缺省值
echo"pleaseinputyouname:
"
readname
echo"===$name=="
echo"younameis....${name:
="ly"}"
echo"==$name=="
―――――――for循环
echo"thisisloopprograme"
forloopin123456
do
echo"$loop"
done
―――――for循环可用用字符串
echo"thisisloopprograme"
forloopinjimmylylzwlzwly56
do
echo"$loop"
done
――――――――用ls作为循环条件
echo"thisisloopprograme"
forloopin`ls-a`
do
echo"$loop"
done
―――――――for循环使用参数,将所有参数作为一个条件。
循环一次
echo"thisisloopprograme"
forloopin"$*"
do
echo"$loop"
done
―――――for循环使用参数,将每个参数作为循环条件,每个参数都循环一次
echo"thisisloopprograme"
forloopin"$@"
do
echo"$loop"
done
―――――――――――用for循环ping机器
echo"thisisloopprograme"
forloopin172.16.1.2172.16.1.45172.16.1.9172.16.1.1
do
ping-c2$loop
done
――――――――for循环备份文件
echo"thisisloopprograme"
forloopin`ls`
do
if[-f$loop]
thencp$loop$loop.bak
fi
done
――――――――――循环将所有文件中的小写换成大写字母
echo"thisisloopprograme"
forloopin`ls`
do
if[-f$loop]
then
cat$loop|tr"[a-z]""[A-Z]"|tee-a./log.log
fi
done
―――――――――until用法,直到用户输入自己的名字后退出
echo"pleaseenteryouname:
"
readname
until["$name"]#判断name是否为空
do
echo"pleaseenteryouname:
"
readname
done
――――――――判断文件是否存在。
如果存在则一直循环。
知道删除了
the_file=/home/jimmy/log.log
until[!
-f$the_file]
do
sleep1
echo"thefileisexsited"
done
echo"thefileisdeleted"
――――――――――――――――while循环
count=1
while[$count-lt1000000]
do
echo"==========$count"
count=`expr$count+1`
done
―――――――――――――――将文件ping.txt中的内容循环读入,并以“。
”分开显示,IFS用来确定命令中什么样的分隔词被发送到原始的UnixBourneshell,并与其他环境变量一样被传递下去
files=$IFS
IFS=.
whilereadNAMEDEPTID
do
echo"$NAME\t$DEPT\t$ID\t====="
done<./ping.txt
IFS=$files
――――――――――――――根据文件的值控制流
files=$IFS
IFS=.
whilereadNAMEDEPTID
do
if["$NAME"='jimmy']
then
echo"thenameis:
$NAME"
exit0
fi
echo"$NAME\t$DEPT\t$ID\t====="
done<./ping.txt
IFS=$files
―――――――循环读入数据,直到输入exit时才退出
echo"pleaseenterthevalue:
"
whilereadvalue
do
if["$value"="exit"]
thenexit1
fi
echo"thevalueis$value"
done
―――――――循环读入文件,以行的方式读入
count=1
whilereadaaaa
do
echo"$aaaa====$count"
count=`expr$count+1`
done<./ping.txt
―――――linux启动的时候自动启动一些服务配置
在/etc/rc.d/rc.local的最后添加要启动的服务即可
/sbin/servicevsftpdstart
/sbin/servicecrondstart
―――――――exec
exec命令:
常用来替代当前shell并重新启动一个shell,换句话说,并没有启
动子shell。
使用这一命令时任何现有环境都将会被清除,。
exec在对文件描述符进行
操作的时候,也只有在这时,exec不会覆盖你当前的shell环境
――――――――IO详解
I/O重定向详解及例子1、基本概念
a、I/O重定向通常与FD有关,shell的FD通常为10个,即0~9;
b、常用FD有3个,为0(stdin,标准输入)、1(stdout,标准输出)、2(stderr,
标准错误输出),默认与keyboard、monitor、monitor有关;
c、用<来改变读进的数据信道(stdin),使之从指定的档案读进;
d、用>来改变送出的数据信道(stdout,stderr),使之输出到指定的档案;
e、0是<的默认值,因此<与0<是一样的;同理,>与1>是一样的;
f、在IO重定向中,stdout与stderr的管道会先准备好,才会从stdin读进资料;
g、管道“|”(pipeline):
上一个命令的stdout接到下一个命令的stdin;
h、tee命令是在不影响原本I/O的情况下,将stdout复制一份到档案去;
i、bash(ksh)执行命令的过程:
分析命令-变量求值-命令替代(``和$())-重
定向-通配符展开-确定路径-执行命令;
j、()将commandgroup置于sub-shell去执行,也称nestedsub-shell,它有
一点非常重要的特性是:
继承父shell的Standardinput,output,anderrorplusany
otheropenfiledescriptors。
k、exec命令:
常用来替代当前shell并重新启动一个shell,换句话说,并没有启
动子shell。
使用这一命令时任何现有环境都将会被清除,。
exec在对文件描述符进行
操作的时候,也只有在这时,exec不会覆盖你当前的shell环境。
2、基本IO
cmd>file把stdout重定向到file文件中
cmd>>file把stdout重定向到file文件中(追加)
cmd1>fiel把stdout重定向到file文件中
cmd>file2>&1把stdout和stderr一起重定向到file文件中
cmd2>file把stderr重定向到file文件中
cmd2>>file把stderr重定向到file文件中(追加)
cmd>>file2>&1把stderr和stderr一起重定向到file文件中
cmdfile2cmd命令以file文件作为stdin,以file2文
件作为stdout
cat<>file以读写的方式打开file
cmdcmd<delimiter分界符
3、进阶IO
>&n使用系统调用dup
(2)复制文件描述符n并把结果用作标准输出
<&n标准输入复制自文件描述符n
<&-关闭标准输入(键盘)
>&-关闭标准输出
n<&-表示将n号输入关闭
n>&-表示将n号输出关闭
上述所有形式都可以前导一个数字,此时建立的文件描述符由这个数字指定而不是缺省
的0或1。
如:
...2>file运行一个命令并把错误输出(文件描述符2)定向到file。
...2>&1运行一个命令并把它的标准输出和输出合并。
(严格的说是
通过复制文件描述符1来建立文件描述符2,但效果通常是合并了两个流。
)
2>