Linux Shell学习(7)

1. 检查变量是否存在,如果不存在,则赋予默认值。
var=${NAME-default}
e.g.

#!/bin/bash                                                                     
argu=${*-noargu}
editor=${EDITOR-emacs}

echo $editor
echo $argu


等价于

#!/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 you

 

2. 如何获得某个文件中的内容
var=$(<filename)
var=$(cat filename)


3. memu box
menu box和input box有点类似,也是用标准错误输出来导出用户的输入选择。
e.g.

 

#!/bin/bash                                                                                                                       

                                      
MAIL="<[email protected]>"
# 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)


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
)

 

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[@]}


7. console management
tput command

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!'   

 

 

 

 

 

 

 

 


                                                                                    Hello World!
root@localhost :/home/James/mypro/algorithm#

 

你可能感兴趣的:(linux,shell,bash)