Please indicate the source: http://blog.csdn.net/gaoxiangnumber1
Welcome to my github: https://github.com/gaoxiangnumber1
\Enter
延伸至下一行。rx
权限): /home/xiang/shell.sh
./shell.sh
bash shell.sh
或sh shell.sh
执行。此时shell.sh只要有r权限即可被执行。撰写第一支script
#!/bin/bash
#Program:
#This program shows "Hello World!" in your screen.
# History:
# 2016-10-12 22:44:04 xiang First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "Hello World!" #echo自动输出换行符
exit 0
#!/bin/bash
宣告本script使用的shell名称。 exit n
使程序中断,返回n给系统。xiang :~ $ sh hello.sh
Hello World!
xiang :~ $ chmod a+x hello.sh
xiang :~ $ ./hello.sh
Hello World!
利用直接执行的方式来执行script
利用source来执行脚本:在父程序中执行
source script_name.sh
在本bash中执行指令,各项动作、变量在本bash内生效。test -e filename
表示是否存在 test -r filename
表示是否可读(root权限例外) test file1 -nt file2
test n1 -eq n2
-n
可省略 #!/bin/bash
# 1. 判断是否输入字符串
read -p "Input filename: " filename
test -z ${filename} && echo "You MUST input a filename." && exit 0
# 2. 判断文件是否存在?若不存在则显示讯息并结束脚本
test ! -e ${filename} && echo "${filename} doesn't exist!" && exit 0
# 3. 判断文件类型与属性
test -f ${filename} && filetype="regulare file"
test -d ${filename} && filetype="directory"
test -r ${filename} && perm="readable"
test -w ${filename} && perm="${perm} & writable"
test -x ${filename} && perm="${perm} & executable"
# 4. 输出信息
echo "${filename} is a ${filetype}"
echo "Permissions for you are: ${perm}"
[□"$HOME"□==□"$MAIL"□]
$ name="xiang gao"
$ [ ${name} == "xiang gao" ]
bash: [: too many arguments
#原因:${name}没有使用双引号括起来,变成[ xiang gao == "xiang gao" ]
$ [ "${name}" == "xiang gao" ]
$ echo $?
0
if then fi
中。完成以下script: #!/bin/bash
read -p "Input Y/y/N/n: " input
test -z ${input} && echo "Must input!" && exit 0
[ "${input}" == "Y" -o "${input}" == "y" ] && echo "Y/y" && exit 0
[ "${input}" == "N" -o "${input}" == "n" ] && echo "N/n" && exit 0
echo "I don't know" && exit 0
/path/to/script_name opt1 opt2 opt3 opt4
$0 $1 $2 $3 $4
#!/bin/bash
echo "The script name is ${0}"
echo "Total parameter number is $#"
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop." && exit 0
echo "Your whole parameter is $@"
echo "The 1st parameter ${1}"
echo "The 2nd parameter ${2}"
$ ./script.sh gao xiang number one
The script name is ./script.sh
Total parameter number is 4
Your whole parameter is gao xiang number one
The 1st parameter gao
The 2nd parameter xiang
shift造成参数变量号码偏移
#!/bin/bash
echo "Total parameter number is $#"
echo "Your whole parameter is '$@'"
shift #第一次shift一个变量
echo "Total parameter number is $#"
echo "Your whole parameter is '$@'"
shift 3 #第二次shift三个变量
echo "Total parameter number is $#"
echo "Your whole parameter is '$@'"
$ ./script.sh 1 2 3 4 5 6
Total parameter number is 6
Your whole parameter is '1 2 3 4 5 6'
Total parameter number is 5
Your whole parameter is '2 3 4 5 6'
Total parameter number is 2
Your whole parameter is '5 6'
$ ./script.sh 1 2 3
Total parameter number is 3
Your whole parameter is '1 2 3'
Total parameter number is 2
Your whole parameter is '2 3'
Total parameter number is 2
Your whole parameter is '2 3'
单层条件判断式
if [ 条件判断式 ]; then
#当条件判断式成立时执行的指令
fi #结束if
&&
(代表AND)或||
(代表or)隔开。#!/bin/bash
read -p "Input Y/y/N/n: " input
if [ "${input}" == "Y" ] || [ "${input}" == "y" ]; then
echo "Y/y"
exit 0
fi
if [ "${input}" == "N" ] || [ "${input}" == "n" ]; then
echo "N/n"
exit 0
fi
echo "I don't know" && exit 0
多重条件判断式
#一个条件判断,分成功进行与失败进行(else)
if [ 条件判断式 ]; then
#条件判断式成立时可以执行的指令
else
#条件判断式不成立时可以执行的指令
fi
#多个条件判断(if ... elif ... elif ... else)分多种不同情况执行
if [ 条件判断式一 ]; then
#条件判断式一成立时可以执行的指令
elif [ 条件判断式二 ]; then
#条件判断式二成立时可以执行的指令
else
#条件判断式一与二均不成立时可以执行的指令
fi
#!/bin/bash
read -p "Input Y/y/N/n: " input
if [ "${input}" == "Y" ] || [ "${input}" == "y" ]; then
echo "Y/y"
elif [ "${input}" == "N" ] || [ "${input}" == "n" ]; then
echo "N/n"
else
echo "I don't know"
fi
exit 0
#!/bin/bash
if [ -z ${1} ]; then
echo "You must input arguments!"
elif [ "${1}" == "hello" ]; then
echo "Hello"
else
echo "Argument must be 'hello'"
fi
exit 0
case $变量名称 in
"第一个变量内容") #用双引号括起来每个变量内容,小括号`)`为关键词
程序段
;; #每个类别结尾使用两个连续分号
"第二个变量内容")
程序段
;;
*) #最后一个变量内容用*代表其他所有值
不包含第一个变量内容与第二个变量内容的其他程序执行段
exit 1
;;
esac #case结尾
#!/bin/bash
case ${1} in
"hello")
echo "hello"
;;
"")
echo "Must input"
;;
*)
echo "Usage ${0} hello"
;;
esac
$变量
有两种获得方式: script.sh variable
的方式来直接设定$变量
的内容。read
指令让用户输入变量的内容。function fun_name() {
程序段
}
#!/bin/bash
function print(){
echo -n "Your choice is " #-n:不断行在同一行显示
}
echo "This program will print your selection."
case ${1} in
"one")
print;
echo ${1} | tr 'a-z' 'A-Z' #将参数做大小写转换
;;
"two")
print;
echo ${1} | tr 'a-z' 'A-Z'
;;
"three")
print;
echo ${1} | tr 'a-z' 'A-Z'
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac
#!/bin/bash
function print(){
echo "Your choice is ${1}" #这个$1必须要参考底下的指令
}
echo "This program will print your selection."
case ${1} in
"one")
print 1; #print指令后面接参数
;;
"two")
print 2
;;
"three")
print 3
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac
$ ./script.sh
Usage ./script.sh {one|two|three}
$ ./script.sh one
Your choice is 1
$ ./script.sh two
Your choice is 2
$ ./script.sh three
Your choice is 3
while [ 判断式 ]
do #do是循环开始
程序段落
done #done是循环结束
until [ 判断式 ] #当判断式成立时终止循环
do
程序段落
done
#让用户输入yes或YES才结束程序,否则一直告知用户输入
#!/bin/bash
while [ "${input}" != "yes" -a "${input}" != "YES" ]
do
read -p "Please input yes/YES to stop this program: " input
done
echo "Input correct"
#!/bin/bash
s=0 #累加和
i=0 #累加变量
while [ "${i}" != "100" ]
do
i=$(($i+1)) #每次i都会增加1
s=$(($s+$i)) #每次都会加和
done
echo "1 + 2 + 3 + ... + 100 = $s"
for var in con1 con2 con3 ...
do
程序段
done
$var
的变量内容在循环时:第一次循环$var
的内容为con1;第二次循环$var
的内容为con2;第三次循环$var
的内容为con3。#!/bin/bash
network="192.168.1" #定义网域前面部分
for num in {1..100}
do
ping -c 1 -w 1 ${network}.${num} &> /dev/null && result=0 || result=1
if [ "${result}" == 0 ]; then
echo "Server ${network}.${num} is UP."
else
echo "Server ${network}.${num} is DOWN."
fi
done
#!/bin/bash
#先判断目录是否存在
read -p "Please input a directory: " dir
if [ "${dir}" == "" -o ! -d "${dir}" ]; then
echo "The ${dir} is NOT exist in your system."
exit 1
fi
#开始测试文件
filelist=$(ls ${dir}) # 列出所有在该目录下的文件名
for filename in ${filelist}
do
perm=""
test -r "${dir}/${filename}" && perm="${perm} readable"
test -w "${dir}/${filename}" && perm="${perm} writable"
test -x "${dir}/${filename}" && perm="${perm} executable"
echo "The file ${dir}/${filename}'s permission is ${perm} "
done
for ((初始值; 限制值; 执行次数))
do
程序段
done
i=1
; i<=100
; i=i+1
。sh [-nvx] scripts.sh
Please indicate the source: http://blog.csdn.net/gaoxiangnumber1
Welcome to my github: https://github.com/gaoxiangnumber1