1. 檢查變量是否存在,若是不存在,則賦予默認值。
var=${NAME-default}
e.g.
shell
#!/bin/bash argu=${*-noargu} editor=${EDITOR-emacs} echo $editor echo $argu
等價於
bash
#!/bin/bash #argu=${*-noargu} [ -z "$*" ] && argu=noargu || argu=$* #editor=${EDITOR-emacs} [ -z $EDITOR ] && editor=noeditor || editor=$EDITOR echo $editor echo $argu
root@localhost :/home/James/mypro/shell/TryOut# ./test.sh
noeditor
noargu
root@localhost :/home/James/mypro/shell/TryOut# ./test.sh hello
noeditor
hello
root@localhost :/home/James/mypro/shell/TryOut# ./test.sh hello nice to meet you
noeditor
hello nice to meet youoop
2. 如何得到某個文件中的內容
var=$(<filename)
var=$(cat filename)ui
3. memu box
menu box和input box有點相似,也是用標準錯誤輸出來導出用戶的輸入選擇。
e.g.spa
#!/bin/bash MAIL="<chen_q07@163.com>" # store menu options selected by the user INPUT=/tmp/menu.sh.$$ # Storage file for displaying 'date' and 'cal' command's output OUTPUT=/tmp/output.sh.$$ # trap and delete tmp files trap 'rm ${INPUT}; rm ${OUTPUT}; exit' SIGHUP SIGINT SIGTERM # # Purpose - display output using msgbox # $1 -> set msgbox height # $2 -> set msgbox width # $3 -> set msgbox title # display_output() { local h=${1-10} local w=${2-40} local t=${3-Output} dialog --backtitle "$MAIL" \ --title "${t}" \ --clear \ --msgbox "$(<$OUTPUT)" ${h} ${w} } # # Purpose - display current system's date and time # show_date() { echo "Today is $(date) @ $(hostname -f)." > $OUTPUT display_output 10 40 "Date and Time" } # # Purpose - display a canlendar # show_cal() { cal > $OUTPUT display_output 20 40 "Calendar" } # # set infinite loop # while true; do ### display main menu ### dialog --clear --help-button --backtitle "$MAIL" \ --title "[ M A I N - M E N U ]" \ --menu "You can use the UP/DOWN array keys, the first \n letter of the choice as a hot key, or the \n\ number keys 1-9 to choose an option.\n\ Choose the TASK" 15 50 4 \ Date/Time "Display date and time" \ Calendar "Display a calendar" \ Exit "Exit to Shell" 2>$"${INPUT}" menuitem=$(<"${INPUT}") # make decisions case $menuitem in Date/Time) show_date;; Calendar) show_cal;; Exit) echo "Bye"; break;; *) echo "NOT RECOGNIZED: ${menuitem}"; break;; esac done # rm temp file if any [ -f ${INPUT} ] && rm ${INPUT} [ -f ${OUTPUT} ] && rm ${OUTPUT}
4. seq命令
for ((i=begin;i<=end;i=i+step)
<==>
for i in $(seq begin step end).net
5. A progress bar
echo percentage | dialog --gauge "text" height width percent
echo "10" | dialog --gauge "Please wait" 10 70 0
echo "50" | dialog --gauge "Please wait" 10 70 0
echo "100" | dialog --gauge "Please wait" 10 70 0
將某個命令的輸出,重定向到gauge box的輸入來控制gaugebox。
dialog --title "Copy file" --gauge "Copying file ..." 10 70 << (
command 1
command 2
...
command N
)code
6. Shell中的array類型的操做,設置和讀取
2031 array=(/bin/* /etc/*)
2032 cat ${#array[*]}
2033 echo ${#array[*]}
2034 for f in ${array[@]}; do echo $f; done
2035 echo ${array[1]}
2036 echo ${array[0]}
2037 echo ${array[100]}
2038 echo ${array[*]}
2039 echo ${array[@]}ci
7. console management
tput commandget
root@localhost :/home/James/mypro/algorithm# tput cols
168
root@localhost :/home/James/mypro/algorithm# tput lines
47
root@localhost :/home/James/mypro/algorithm# tput cup $(($(tput lines)/2)) $(($(tput cols)/2)); echo 'Hello World!' input
Hello World!
root@localhost :/home/James/mypro/algorithm#