65.IFS:文本分隔符
默认的文本分隔符是’ ',但是可以手动设置为其他的,如:‘:’ ‘|’ ': ’
cities=Delhi:chennai:bangaluru:kolkata
old_ifs="$IFS"
IFS=":"
for place in $cities
do
echo the name of city is $place
done
66.tr 字符替换
tr 'abcd' 'efgh' #一一对应的,将a->b b->f c->g
67.函数定义
function function_name(){
}
function_name(){
}
查看当前shell环境中定义的函数:
declare -f
清除当前shell中的函数:
unset -f function_name
yesno(){
while [[ true ]]; do
echo "$*"
echo "please answer by entering yes or no:"
read reply
case $reply in
yes)
echo "you answer yes"
return 0
;;
no)
echo "you answered no"
return 1
;;
*)
echo "invalid input"
;;
esac
done
}
yesno
如何在函数中共享变量
temp="/temp/filename"
remove_file(){
echo "removing file $temp...."
}
remove_file
68.local:本地局部变量
函数本地变量设置:
在上述程序段中,temp变量可能会被任意函数修改,如果我们希望获得一个只在本函数起作用的变量,那么加上local关键字就好。
name="john"
hello(){
local name="Mary"
echo $name
}
echo $name
hello
echo $name
69.函数返回值。
函数只能返回整数,如果想返回字符串啥的,需要将值存储在一个全局变量中,或者输出到临时file中。
如果不在函数中使用return,那么函数执行的最后一行命令是否成功就会被作为返回值返回。
is_user_root(){
[ $(id -u) -eq 0 ];
}
is_user_root && echo "you are root user, you can go ahead." || echo "you need be administrator to run this script"
70.函数也可以被放置到后台执行。
dobackup(){
echo "started backup"
tar -zcvf /dev/st0 /home >/dev/null 2>&1
echo "completed backup"
}
dobackup &
echo -n "task..done."
echo
输出:
task..done
started backup
completed backup
所以将任务放到后台后,将相当于执行了异步操作。
71.source关键字。
避免创建新的shell环境,直接在当前shell环境中运行shell命令。这样当前的所有变量以及函数都可以在新的shell脚本中可用。
语法格式:
source filename [arguments]
或者
. filename [arguments]
source关键字可以用来加载shell脚本库。那么脚本中的所有函数和变量均可以在当前shell中使用。
72.中断与陷阱:trap
语法格式:
trap 'command; command' signal-name
trap 'command; command' signal-number
例子:
trap 'echo "you pressed control key"' 0 1 2 15
或者
trap " " INT QUIT TSTP #表示忽略这几个中断。
命令行输入 kill -l 就会显示所有的中断名字及中断号
恢复默写信号的默认行为:
trap signalName or signal number
捕捉ctrl+c ctrl+\信号
trap "echo caught signal sigint" SIGINT
trap "echo caught signal SIGQUIT" 3
trap "echo caught signal SIGTERM" 15
trap "echo caught signal SIGTSTP" TSTP
echo "enter any string (type 'dough' to exit)."
while [[ true ]]; do
echo "rolling...\c"
read string
if [[ "$string" = "dough" ]]; then
break
fi
done
echo "existing normally"
73.dialog
终端弹出dialog。
例子:
dialog --msgbox "This is a message." 10 50
弹出选择dialog,并相应用户的选择操作。
dialog --title "Delete file" \
--backtitle "Learning Dialog Yes-No box" \
--yesno "Do you want to delete file \" ~/work/sample.txt\"?" 7 60
#selecting "yes" button will return 0 这个很关键。
#selecting "No" button will return 1.
#selecting "ESC" will return 255.
result=$?
case $result in
0 ) rm ~/work/sample.txt
echo "File deleted."
;;
1) echo "File could not be deleted.";;
255) echo "action cancelled - Pressed [ESC] key.";;
esac
输出图形界面如下:
───────────────────────────────────────────────────────────
┌──────────────────────Delete file─────────────────────────┐
│ Do you want to delete file " ~/work/sample.txt"? │
│ │
│ │
├──────────────────────────────────────────────────────────┤
│ < Yes > < No > │
└──────────────────────────────────────────────
74. >$ 创建文件。
fileName #会创建一个文件名为filename的空文件
语法格式:command > filename
75. inputBox:弹出输入对话框。
result="output.txt"
> $result #创建一个空文件
dialog --title "inputbox demo" \
--backtitle "learn shell scripting" \
--inputbox "please enter your name " 8 60 2>$result
response=$?
name=$(<$result)
case $response in
0 ) echo "hello $name"
;;
1 ) echo "cancelled."
;;
255) echo "escape key pressed."
;;
esac
rm $result
76.textbox:文本显示对话框。
#将文件/etc/passwd中的内容显示在textbox中
dialog --textbox /etc/passwd 10 50
77.密码输入框:insecure.
当要求用户输入密码时,以*显示输入的密码。
#creating the file to store password
result="output.txt"
touch $result 2>/dev/null
#delete the password stored file, if program is existed pre-maturely
trap "rm -f output.txt" 2 15 #这里的考虑也比较周到,捕捉一些退出的信号量,保证密码文件能被删除
dialog --title "password" \ #注意:反斜杠与命令选项间需要加空格。
--insecure \
--clear \
--passwordbox "please enter password" 10 30 2> $result
reply=$?
case $reply in
0 ) echo "you have entered password: $(cat $result)"
;;
1) echo "you have pressed cancel"
;;
255) cat $data && [ -s $data ] || echo "escape key is pressed."
;;
esac
78.menubox:用户选择输入框。
如下例子:弹出一个列表选项,用户可选择其中任意一项,然后根据用户的选择,程序作出相应的反应。
RESPONSE=menu.txt
TEMP_DATA=output.txt
vi_editor=vi
trap "rm $TEMP_DATA;rm $RESPONSE;exit" SIGHUP SIGINT SIGTERM
#同理,这里的考虑也比较周到,捕捉信号删除文件。
function display_output() {
dialog --backtitle "learning shell scripting" --title "output" --clear --msgbox "$(<$TEMP_DATA)" 10 41
}
function display_date() {
echo "today is `date` @ $(hostname -f)." > $TEMP_DATA
display_output 6 60 "date and time"
}
function display_calendar() {
cal > $TEMP_DATA
display_output 13 25 "Calendar"
}
while [[ true ]]; do
dialog --help-button --clear --backtitle "learn shell scripting" \
--title "[ Demo Menubox ]" \
--menu "Please use up/down arrow keys, number keys\n\
1,2,3..,or the first character of choice \n\
as hot key to select an option" 15 40 4 \ #menu后面跟的是选项。
Calendar "Show the Calendar" \
Date/time "show date and time" \
Editor "start vi editor" \
Exit "Terminate the script" 2>"${RESPONSE}"
menuitem=$(<"${RESPONSE}")
case $menuitem in
Calendar )
display_calendar
;;
Date/time ) display_date ;;
Editor) $vi_editor;;
Exit) echo "thank you!";break;;
esac
done
[ -f $TEMP_DATA ] && rm $TEMP_DATA
[ -f $RESPONSE ] && rm $RESPONSE
多选框:checklist box
#按方向键切换选项,按空格键切换选中转态
dialog --checklist "this is a checklist" 10 50 2 \
"a" "this is one option" "off" \
"b" "this is the second option" "on"
单选框:radiolist box
dialog --radiolist "this is selective list, where only one \
option can be chosen" 10 50 2 \
"a" "this is the first option" "off" \
"b" "this is the second option" "on"
进度条:gauge
declare -i COUNTER=1
{
while test $COUNTER -le 100
do
echo $COUNTER
COUNTER=COUNTER+1
sleep 1
done
} | dialog --gauge "this is a progress bar" 10 50 0