控制流结构
来源:互联网 发布:光棍节 知乎 编辑:程序博客网 时间:2024/05/23 00:10
1.退出状态
exit n
0 ----退出成功
1 ----退出失败
2.控制结构
if 条件1
then 命令1
elif 条件2
then 命令2
else 命令3
fi
#!/bin/sh
#iftest.txt
if [ 10 -lt 12 ]
then
echo "Yes,10 is less than 12"
else
echo "No,10 is more than 12"
fi
#!/bin/sh
#iftest2.txt
echo -n "Enter your name :"
read NAME
if [ ${NAME} ="" ]
then
echo "You did not enter any information"
fi
[root@localhost yjg]# ./iftest2.txt
Enter your name :
You did not enter any information
3.grep输出检查
#!/bin/sh
#grepif.txt
if grep 'yjg/>' yjg.txt > /dev/null 2>&1
then
echo "Great yjg is in the file"
else
echo "No yjg is not in the file"
fi
4.用变量测试输出
#!/bin/sh
#grepstr.txt
echo -n "Enter a list of names:"
read LIST
if echo ${LIST} | grep "Peter" > /dev/null 2>&1
then
echo "Peter is here"
else
echo "Peter's not in the list. No comment!"
fi
5.文件拷贝测试
#!/bin/sh
#ifcp.txt
if cp yjg2.txt yjg_bak.txt >/dev/null 2>
then
echo "good copy"
else
echo "`basename $0`:error could not copy the files" >&2
fi
6.当前目录测试
#!/bin/sh
#ifpwd.txt
DIRECTORY=`pwd`
echo $DIRECTORY
if [ "$DIRECTORY" != "/" ]
then
echo "Your run in the root diretory" >&1
exit 1
else
echo "root directory"
exit 0
fi
7.测试传递到脚本的参数个数
#!/bin/sh
#ifparam
if [ $# -lt 3 ]
then
echo "Usage: ` basename $0 ` arg1 arg2 arg3" >&2
exit 1
fi
echo "arg1: $1"
echo "arg2: $2"
echo "arg3: $3"
8.测试脚本是否为交互模式
#!/bin/sh
#ifinterative.txt
if [ -t ]
then
echo "We are interactive with a terminal"
else
echo "We must be running from some background process probably cron or at"
fi
~
9.变量设置测试
#!/bin/sh
if [ -z $EDITOR ]
then
echo "Your EDITOR environment is not set"
else
echo "Using $EDITOR as the default editor"
fi
10.检测执行脚本用户
#!/bin/sh
#ifroot.txt
if [ "$LOGNAME" != "root" ]
then
echo "You need to be root run this script" >&2
exit 1
else
echo "Yes indeed you are $LOGNAME proceed"
fi
11.将脚本参数传入系统命令
#!/bin/sh
#ifdirec.txt
DIRCTORY=$1
if [ "`ls -A $DIRCTORY`" = "" ]
then
echo "$DIRCTORY is indeed empty"
else
echo "$DIRCTORY is not empty"
fi
12.null的用法
#!/bin/sh
#ifmkdir.txt
DIRECTORY=$1
if [ "$DIRECTORY" = 1 ]
then
echo "Usage:`basename $0` directory to create " >&2
exit 1
fi
if [ -d $DIRECTORY ]
then
: #do nothing
else
echo "The directory does exist"
echo -n "Create it now? [Y..N] : "
read ANS
if [ "$ANS" = "Y" ] || [ "$ANS" = "y" ]
then
echo "creating now"
mkdir $DIRECTORY > /dev/null 2>&1
if [ $? != 0 ]
then
echo "Errors creating the directory $DIRECTORY" >&2
exit 1
fi
else : #do nothing
fi
fi
#!/bin/sh
#ifcp1.txt
if cp $1 $2 > /dev/null 2>&1
then
:
else
echo "`basename $0` : ERROR failed to cp $1 to $2 "
exit 1
fi
13.增加和检查整数值
[root@localhost yjg]# cat count.txt
#!/bin/sh
#count.txt
COUNTER=100
echo "Do you wish to change the counter value currently set a $COUNTER ? [y .. n] "
read ANS
if [ $ANS = "y" ] || [ $ANS ="Y" ]; then
echo "Enter your to change the value"
read VALUE
expr $VALUE + 10 > /dev/null 2>&1
STATUS=$?
if [ "$STATUS" = "" ] || [ "$STATUS" != "0" ]; then
echo " You either entered nothing or non-numeric " >&2
echo " Sorry now exiting ... counter stays at $COUNTER" >&2
exit 1
fi
COUNTER=`expr $COUNTER + $VALUE`
echo " Counter now set to $COUNTER "
else
echo "Counter stays at $COUNTER"
fi
14.简单的安全登录脚本
#!/bin/sh
#ifpass.txt
INVALID_USER=yes
INVALID_PASSWD=yes
SAVEDSTTY=`stty -g`
echo "You are logging into a senstive area"
echo -n "Enter your ID name :"
read NAME
stty -echo
echo "Enter your password :"
read PASSWORD
stty $SAVEDSTTY
if [ "$NAME" = "dave" ]; then
INVALID_USER=no
fi
if [ "$PASSWORD" = "myday" ]; then
INVALID_PASSWD=no
fi
if [ "$INVALID_USER" = "yes" -o "$INVALID_PASSWD" = "yes" ]; then
echo " `basename $0 :` Sorry wrong password or userid"
exit 1
fi
echo "Correct user id and password given"
15.if then...elif then ...else fi
#!/bin/sh
#ifcataudit.txt
LOCAT_1=/usr/local/yjg/12.log
LOCAT_2=/usr/local/yjg/11.log
if [ -r $LOCAT_1 ]; then
echo "Using LOCAT_1"
cat $LOCAT_1
elif [ -r $LOCAT_2 ]; then
echo "Using LOCAT_2"
cat $LOCAT_2
else
" `basename $0` : Sorry the audit file is not readable or cannot be located." >&2
exit 1
fi
16.case 选择
#!/bin/sh
#caseselect.txt
echo -n "enter a number from 1 to 5 :"
read ANS
case $ANS in
1) echo "you select 1"
;;
2) echo "you select 2"
;;
3)echo "you select 3"
;;
4)echo "you select 4"
;;
5)echo "you select 5"
;;
*) echo " `basename $0` : This is not between 1 and 5 " >&2
exit 1
;;
esac
#!/bin/sh
#caseterm.txt
echo " choices are .. vt100,vt102,vt220"
echo -n "enter your terminal type:"
read TERMINAL
case $TERMINAL in
vt100|vt102 ) TERM=100
echo "term 100 102"
;;
vt220 ) TERM=220
echo "term 220"
;;
* )
echo " `basename $0` : Unknown response " >&2
echo "setting it to vt100,so there"
TERM=vt100
;;
esac
export TERM
echo "Your terminal is set to $TERM"
17.case 与命令参数传递
#!/bin/sh
#caseparam.txt
if [ $# != 1 ]; then
echo "Usage: `basename $0` [start|stop|help] " >&2
exit 1
fi
OPT=$1
case $OPT in
start)
echo "starting.. `basename $0`"
;;
stop)
echo "stopping.. `basename $0`"
;;
help)
;;
*)
echo "Usage: `basename $0` [start|stop|help]"
;;
esac
18.
#!/bin/sh
#caserep.txt
echo " Weekly Report"
echo -n "What day do you want to run report [Saturday] :"
read WHEN
echo "validating..${WHEN:="Saturday"}"
echo "${WHEN}"
case $WHEN in
Monday)
;;
Sunday)
;;
*)
echo " Are you nuts!,this report can only be run on" >&2
echo " on a Saturday,Sunday or Monday" >&2
exit 1
;;
esac
19.for 循环
对for循环使用ls命令
#!/bin/sh
#forls.txt
for loop in ` ls `
do
echo $loop
done
20.对for循环使用参数
连接服务器
#!/bin/sh
#forping.txt
HOSTS="itserv dnssevr acctsmain ladpd ladware"
for loop in $HOSTS
do
ping -c 2 $loop
done
备份多个文件
#!/bin/sh
#forUC.txt
for files in ` ls *.log `
do
cp $files '/usr/local/yjg/nec' > /dev/null 2>&1
done
大小写转化
#!/bin/sh
#forUC.txt
for files in ` ls *.log `
do
cat $files | tr "[ a-z ]" "[ A-Z ]" > $files.UC
done
#!/bin/sh
#forsed.txt
for files in `ls *.log`
do
sed -e "/^$/d" $files >$files.HOLD
mv $files.HOLD $files
done
break ,continue
#!/bin/sh
#breakout.txt
while :
do
echo -n "Enter any number [ 1..5 ] :"
read ANS
case $ANS in
1|2|3|4|5)
echo "great you entered a number between 1 and 5"
;;
*)
echo "Wrong number .. bye"
break
esac
done
#!/bin/sh
#whilecontinue.txt
SAVEDIFS=$IFS
IFS=:
INPUT_FILE=name2.txt
NAME_HOLD="Peter James"
LINE_NO=0
if [ -s $INPUT_FILE ]; then
while read NAME DEPT ID
do
LINE_NO=` expr $LINE_NO + 1 `
if [ $LINE_NO -le 2 ]; then
continue
fi
if [ "$NAME_HOLD" = "$NAME" ]; then
continue;
else
echo " Now processing ... $NAME $DEPT $ID "
fi
done < $INPUT_FILE
IFS=$SAVEDIFS
else
echo " `basename $0 ` : Sorry file not found or there is no data in the file " >&2
exit 1
fi
菜单
#!/bin/sh
#menu.txt
MYDAY=`date +%d/%m/%Y`
THIS_HOST=`hostname -s`
USER=`whoami`
while :
do
tput clear
cat << MAYDAY
-----------------------------------------------------------
User: $USER Host:$THIS_HOST Date:$MYDATE
------------------------------------------------------------
1: List files in current directory
2: Use the vi editor
3: See who is one the system
H: Help screen
Q: Exit menu
-----------------------------------------------------------
MAYDAY #---------------前不能有空格
echo -e -n "/tYou Choice [1,2,3,H,Q] >"
read CHOICE
case $CHOICE in
1) ls
;;
2) vi
;;
3) who
;;
H|h)
cat << MAYDAY
This is the help screen,nothing here yet to help you!
MAYDAY #---------------前不能有空格
;;
Q|q)
exit 0
;;
*)
echo -e "/t/007unknown user response"
;;
esac
echo -e -n "/tHit the return key to continue"
read DUMMY
done
- Shell控制结构--流控制
- 控制流结构
- 控制流结构
- shell控制流结构
- 控制流结构
- 控制流结构
- 控制流结构
- Shell控制流结构笔记
- 控制结构
- 控制结构
- 控制结构
- 控制结构
- 控制结构
- 控制结构
- shell_04控制流结构_笔记
- shell脚本中的控制流结构
- 控制流结构--shell编程笔记
- shell脚本中的控制流结构
- DTD语法概述
- seo方法,搜索引擎优化方法收集
- 暂空
- 异常:java.lang.NoSuchMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext
- XML Schema 简介
- 控制流结构
- MFC 运行批处理 转
- windows消息处理机制和VB
- 修改myelipse中部署路径deploy location内容的方法
- 解码 XML 和 DTD
- .NET播放音乐
- 收藏的HTML编辑器集合 连接
- 项目组成和开发步骤——VB
- linux高级工具命令(三)使用gprof和oprofile查找性能瓶颈