bash shell.sh或者sh shell.sh执行#!/bin/bash
#Program:
# This program shows "hello world!" on the screen.
#Version
# 0.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
export PATH
echo "hello junzerg!"
exit 0程序由一下几个部分组成:
shift命令可以造成参数变量,拿掉前面那个参数。如果加上数字作为参数的话,可以拿掉最前面的n个参数。 例子:
#!/bin/bash
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> $@"
shift
echo "Total parameter number is ==> $#"
echo "Yore whole paramter is ==> $@"
shift
echo "Total parameter number is ==> $#"
echo "Yore whole paramter is ==> $@"
shift
echo "Total parameter number is ==> $#"
echo "Yore whole paramter is ==> $@"
shift执行可以看到:
[root@iZwz9ayvrn26ptadymu9ymZ shell]# sh shift_paras.sh one two three four five
Total parameter number is ==> 5
Your whole parameter is ==> one two three four five
Total parameter number is ==> 4
Yore whole paramter is ==> two three four five
Total parameter number is ==> 3
Yore whole paramter is ==> three four five
Total parameter number is ==> 2
Yore whole paramter is ==> four five!#/bin/bash
#输入文件名,判断使用者是否有输入字符串
echo -e "Please input a filename ,I will check the file's type and permission.\n"
read -p "Input a filename:" filename
test -z ${filename} && echo "You must input a filename." && exit 0
# 判断文件是否存在
test ! -e ${filename} && echo "The filename '${filename}' do not exist" && exit 0
# 判断文件类型与属性
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"
echo "The filename:${filename} is a ${filetype}"
echo "And the permissions for you are : ${perm}" 例子:
#!/bin/bash
#blah blah
read -p "Please input (Y/N):" yn
[ "${yn}" == "Y" -o "{$yn}" == "y" ] && echo "OK, continue!" && exit 0
[ "${yn}" == "N" -o "{$yn}" == "n" ] && echo "Oh,interrupt!" && exit 0
echo "I don't konw what your choice is " && exit 0if[ 条件判断 ]; then
当条件成立时,可以进行的工作
fi在使用中括号作为条件判断时可以:
[ "${yn}" == "Y" -o "${yn}" == "y"]
[ "${yn}" == "Y" ] || [ "${yn}" == "y" ]if [ 条件判断式 ]; then
当条件判断成立时,执行的指令
else
当条件判断不成立时,执行的指令
fi更复杂的情况为:
if [ 条件判断式一 ]; then
当条件判断式一成立时,执行的指令
elif [ 条件判断式二 ]; then
当条件判断式二成立时,执行的指令
else
当条件判断式一和二都不成立时,执行的指令
ficase $变量名称 in
"第一个变量内容")
指令
;;
"第二个变量内容")
指令
;;
*)
不包含第一个变量内容和第二个变量内容时执行的指令
exit 1
;;
esac例如:
#!/bin/bash
#blah blah
case ${1} in
"a")
echo "Hello ${1}"
;;
"b")
echo "${1} Hello"
;;
[0-9])
echo "Hello number"
;;
*)
echo "Usage ${0} {hello}"
;;
esac当condition条件成立时,就进行循环,直到condition的条件不成立。
while [ condition ]
do
指令
done当condition条件不成立时,进行循环,直到condition条件成立。
until [ condition ]
do
指令
done数组中的循环
for var in con1 con2 con3 ...
do
指令
done例子一:
echo -e "Animals:"
for animal in dog cat mouse
do
echo "There is a ${animal}."
done运行结果为:
Animals:
There is a dog.
There is a cat.
There is a mouse.可以使用Linux内建机制处理,例如seq命令和...:
for i in $(seq 1 100)
for i in $(seq a...g)函数的语法:
function fname() {
指令
}注意:
shell执行可以通过参数的设定来debug
sh [-nvx] scripts.sh
-n :不要执行script,仅查询语法
-v :执行script钱,现将script内容输出到屏幕
-x : 将使用到的script内容显示到屏幕上