1 shell脚本执行参数
1.1 -v参数
bash -v sum.sh表示将代码执行之前,先将代码显示出来
[gexing111@gexing111 myapps]$ bash -v sum.sh
#!/bin/bashlet a=30
let b=20
let sum="$a + $b"
if test $a -lt $b
then
echo "OK"
else
echo "NO"
fi
NO
1.2 -x参数
bash -x sum.sh表示将代码执行过程中显示出来
+ let a=30
+ let b=20
+ let 'sum=30 + 20'
+ test 30 -lt 20
+ echo NO
NO
2 shell脚本程序
2.1 输入密码无回显
#!/bin/bash
#Filename: password.sh
echo -e "Enter your password :"
stty -echo
read password
stty echo
echo "Password read!"
其中stty是终端处理工具,其选项-echo表示禁止将输出发送到终端;而选项echo表示允许发送输出。
2.2 光标计时
#!/bin/sh
echo -n Count
tput sc
count=0
while true
do
if [ $count -lt 40 ]
then
let count++
sleep 1
tput rc
tput ed
echo -n $count
else
exit 0
fi
done
我们使用tput sc存储光标位置,tput rc恢复光标位置,tput ed清除从当前光标位置之后的所有内容。
2.3 图片批量重命名
#!/bin/bash
#文件名: rename.sh
#用途: 重命名 .jpg 和 .png 文件
count=1;
for img in `find . -iname '*.png' -o -iname '*.jpg' -type f -maxdepth 1`
do
new=image-$count.${img##*.}
echo "Renaming $img to $new"
mv "$img" "$new"
let count++
done
该脚本将当前目录下所有的 .jpg 和 .png 文件重命名,新文件名的格式为image-1.jpg、image-2.jpg、image-3.jpg、image-4.png等,依次类推。
程序2:readEmployees.sh
#!/bin/bash
clear
echo "Plz input your Name"
read Name
echo "Plz input your Age"
read Age
echo "Name:$Name Age:$Age">>mydata.dat
echo ""
echo "Employees:"
echo ""
cat mydata.dat
程序3:sum.sh
#!/bin/bash
let a=30
let b=20
let sum="$a + $b"
if test $a -lt $b
then
echo "OK"
else
echo "NO"
fi
程序4:mymenu.sh
#!/bin/bash
declare flag="1"
while [ $flag -eq "1" ]
do
echo "The Telephone Book"
echo ""
echo "1.Display A Telephone Number"
echo "2.Add A New Telephone NUmber"
echo ""
echo "Q Quit"
echo ""
echo "Enter your selection:"
read selection
case $selection in
"1")
echo "You want to display a telephone number."
getnum
;;
"2")
echo "You want to add a new telephone number."
addnum
;;
"q")
flag="0"
;;
"Q")
flag="0"
;;
*)
echo "You made an invalid selection."
esac
done
5,程序friends.sh
#!/bin/bash
for friend in "Mary Jones" "Joe Smith" "Sue Jones"
do
echo "Hello,$friend"
done
6,程序raining.sh
#!/bin/bash
declare raining="1"
while [ $raining -eq "1" ]
do
clear
echo ""
echo "Is it raining?"
echo ""
echo "1,YES"
echo "2,NO"
echo ""
echo "Enter your selection:"
read raining
done
echo "It stopped raining."
7,程序blink.sh
#!/bin/bash
clear
declare count1=1
declare count2
while [ $count1 -lt 6 ]
do
echo "Warning:There is a bug in your program!"
let count2=1
while [ $count2 -lt 20000 ]
do
let count2="$count2 + 1"
done
clear
let count2=1
while [ $count2 -lt 20000 ]
do
let count2="$count2 + 1"
done
let count1="$count1 + 1"
done
8,程序break.sh
#!/bin/bash
let n=1
while [ $n -eq 1 ]
do
echo "Enter your name or \"stop\" to end:"
read name
case $name in
"stop")
echo "Bye!"
break
;;
*)
echo "Hi,$name!"
;;
esac
done
9,程序function_dis.sh
#!/bin/bash
clear
function display
{
echo "Welcome to the world"
echo "of functions."
}
display
10,程序function_verify.sh
#!/bin/bash
clear
function verify
{
if [ $# -ne 2 ]
then
echo "Wrong number of arguments!"
#字符串的比较用= 而数字的比较用-eq
elif [ $1 = "gexing111" ] && [ $2 = "111gexing" ]
then
echo "Verified"
else
echo "Rejected"
fi
}
verify $1 $2