shell脚本
shell就是我们与系统沟通的一个接口,shell脚本呢就是利用shell的功能所写的一个程序-------纯文本文件(shell语法,命令,正则表达式,管道,数据重定向)
注意:命令自上而下,从左而右执行,空白行空格都会被忽略。如果读取到enter,就尝试执行该行命令;#可以作为注释
执行:
#直接命令执行:shell.sh文件必须要具备可读可执行权限,然后:
#绝对路径:使用/home/dmtsai/shell.sh
#相对路径:使用./shell.sh
#变量[PATH]:shell.sh放在PATH指定目录,如~/bin/
#以bash程序来执行,bash shell.sh或sh shell.sh
编写第一个脚本
mkdir bin;cd bin
vim hello.sh
#!/bin/bash /声明这个脚本使用的shell名称 #!开头被称为shebang行
#program:
# this program shows "hello world" in your screen
#history
#2022-08-16
PATH=/bin:/sbin:/usr/bin:/sur/sbin:/usr/local/bin:/usr/local/sbin:~/bin /主要环境变量声明
export PATH
echo -e "hello world!\a \n" /主程序
exit 0 /执行结果告知,返回值,还可以自定义错误信息
简单的shell
vim showname.sh
#!/bin/bash
#program:
# user input first name and last name,program shows full name
#history
#2022-08-16 xhd
PATH=/bin:/sbin:/usr/bin:/sur/sbin:/usr/local/bin:/usr/local/sbin:~/bin /主要环境变量声明
export PATH
read -p "please input you first name:" firstname
read -p "please input you last name:" lastname
echo -e "\nYour full name is :${firstname} ${lastname}"
vim create_3_filename.sh
#!/bin/bash
#program:
# program creates 3 files,which named by user's input and command
#history
#2022-08-16 xhd
PATH=/bin:/sbin:/usr/bin:/sur/sbin:/usr/local/bin:/usr/local/sbin:~/bin /主要环境变量声明
export PATH
echo -e "i will use touch command to create 3 files"
read -p "please input your filename:" fileuser
filename=${fileuser:-"filename"} #判断是否有配置文件名
date1=$(date --date='2 days ago'+%Y%m%d) /前两天的日期
date2=$(date --date='1 days ago'+%Y%m%d) /前一天的日期
date3=$(date +%Y%m%d) /今天的日期
file1=${filename}${date1} /配置文件名
file2=${filename}${date2}
file3=${filename}${date3}
touch "${file1}"
touch "${file2}"
touch "${file3}"
在运算时建议:var=$((运算内容))
vim multiplying.sh
#!/bin/bash
#program:
# user inputs two numbers;program will cross two numbers
#history
#2022-08-16 xhd
PATH=/bin:/sbin:/usr/bin:/sur/sbin:/usr/local/bin:/usr/local/sbin:~/bin /主要环境变量声明
export PATH
echo -e "please input 2 numbers,i will multiply them"
read -p "first number:" firstnum
read -p "second number:" secondnum
total=$((${firstnum}*${secondnum}))
echo -e "\nThe result is===>${total}"
脚本执行方式差异(source,sh script,./script)
当直接执行脚本文件时,系统会给我们一个新的bash来执行showname.sh里面的命令,因此firstname这些变凉了是在子进程bash内执行,当脚本文件执行结束,所有子进程的数据被删除,因此父进程下echo变量,看不到任何东西
利用source来执行脚本,在父进程执行
善用判断式:
test,检查某些文件或相关属性
test -e /xhd
test -e /xhd && echo "exist" || echo "not exist"
利用判断符号[]
如果想知道${HOME}是否为空
[ -z “${HOME}” ]; echo $?
中括号两端需要空格来分隔
中括号内的每个组件都需要空格来分隔
中括号内的变量,最好都用双引号括号括起来
常数,用单引号或双引号括号括起来
shell脚本默认变量:
vim how_paras.sh
#!/bin/bash
#program:
# program shows the script name,parameters...
#history
#2022-08-16 xhd
PATH=/bin:/sbin:/usr/bin:/sur/sbin:/usr/local/bin:/usr/local/sbin:~/bin /主要环境变量声明
export PATH
echo "the script name is : ====>${0}"
echo "the total parameters is : ===>$#"
[ "$#" -lt 2 ] && echo "the number of parameter is less than 2.stop here." && exit 0
echo "the whole parameters: ===> '$@'"
echo "the first ==> ${1}"
echo "the second ==> ${2}"
shift 就是偏移
条件判断式
if …then
单层简单判断
if [statement];then
条件成立,执行内容
fi /结束
vim ans_yn.sh
#!/bin/bash
#program:
# shows the user's choice
#history
#2022-08-16 xhd
PATH=/bin:/sbin:/usr/bin:/sur/sbin:/usr/local/bin:/usr/local/sbin:~/bin /主要环境变量声明
export PATH
read -p "please input(Y/N):"yn
if [ "${yn}" == "Y"] || [ "${yn}" == "y"]; then
echo "ok continue"
exit 0
fi
if [ "${yn}" == "N"] || [ "${yn}" == "n"]; then
echo "ok interrupt"
exit 0
fi
echo "i don't know your choice" && exit 0
多重判断
if [statement];then
条件成立,执行内容
else
条件不成立,执行
fi /结束
if [statement1];then
条件成立,执行内容
elif[statement2];then
条件2成立,执行
else
条件不成立,执行
fi /结束
vim hello_2.sh
#!/bin/bash
#program:
# check $1 equal 'hello'
#history
#2022-08-16 xhd
PATH=/bin:/sbin:/usr/bin:/sur/sbin:/usr/local/bin:/usr/local/sbin:~/bin /主要环境变量声明
export PATH
if [ "${1}" == "hello"]; then
echo "hello xhd"
elif [ "${1}" == "" ]; then
echo "you must input parameter,ex> {${0} someword}
else
echo "you must input hello,ex> {${0} hello}
fi
case...esac
image.png
vim hello_3.sh
#!/bin/bash
#program:
# show hello from $1 by using case...esac
#history
#2022-08-16 xhd
PATH=/bin:/sbin:/usr/bin:/sur/sbin:/usr/local/bin:/usr/local/sbin:~/bin /主要环境变量声明
export PATH
case ${1} in
"hello")
echo "hello xhd"
;;
"")
echo "you must input parameter,ex> {${0} someword}
;;
*)
echo "Usage ${0} {hello}"
;;
esac
function:程序最前面
function fname() {
程序段
}
vim show123-2.sh
#!/bin/bash
#program:
# use function to repeat information
#history
#2022-08-16 xhd
PATH=/bin:/sbin:/usr/bin:/sur/sbin:/usr/local/bin:/usr/local/sbin:~/bin /主要环境变量声明
export PATH
function printit(){
echo -n "your choice is:" /加上-n 可以不换行,一行显示
}
echo "this program will print your choice"
case ${1} in
"one")
printit; echo ${1} | tr 'a-z' 'A-Z'
;;
"two")
printit; echo ${1} | tr 'a-z' 'A-Z'
;;
"three")
printit; echo ${1} | tr 'a-z' 'A-Z'
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac
function 可以用来简化代码,内置变量类似脚本shell,函数名称代表式$0,后续依次是$1,$2…
循环loop
常见循环
while [ condition ] <===中括号内就是判断式
do /开始
程序段落
done
until [ condition ]
do /开始
程序段落
done
vim cal_1_100.sh
#!/bin/bash
#program:
# use loop calculate "1+2+....+100"
#history
#2022-08-16 xhd
PATH=/bin:/sbin:/usr/bin:/sur/sbin:/usr/local/bin:/usr/local/sbin:~/bin /主要环境变量声明
export PATH
s=0 # 求和数值变量
i=0 # 累计的数值1,2,3....
while ["${i}" != "100"}]
do
i=$(($i+1))
s=$(($s+$i))
done
echo "the result is: ===> $s"
for ...do...done
for var in con1,con2,con3...
do
程序段
done
vim dir_perm.sh
#!/bin/bash
#program:
# use input dir name,i find the permission of files
#history
#2022-08-16 xhd
PATH=/bin:/sbin:/usr/bin:/sur/sbin:/usr/local/bin:/usr/local/sbin:~/bin /主要环境变量声明
export PATH
read -p "please input a dir:" dir
if [ "${dir}" == "" -o ! -d "${dir}" ]; then
echo "the ${dir} is not exist"
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} writeable"
test -x "${dir}/${filename}" && perm="${perm} executable"
echo "the file${dir}/${filename}'s permission is ${perm}"
done
for((初始值;限制值;赋值运算))
do
程序段
done
shell脚本跟踪与调试
sh [-nvx] script.sh
-n 不执行,仅查询语法问题
-v 执行前,将脚本内容输出到屏幕
-x 将使用到的脚本内容显示出来