2014-09-21 46 views
0

我正在学习bash。我遇到了一些问题。 “文件”看起来不像一个变量,看起来像一个命令 只是无法理解的东西[] 任何帮助将不胜感激 原始代码在这里。一些bash脚本回声的含义“用法:sysinfo_page [[[-f file] [-i]] | [-h]]”

#!/bin/bash 

# sysinfo_page - A script to produce a system information HTML file 

##### Constants 

TITLE="System Information for $HOSTNAME" 
RIGHT_NOW=$(date +"%x %r %Z") 
TIME_STAMP="Updated on $RIGHT_NOW by $USER" 

##### Functions 

system_info() 
{ 
    echo "<h2>System release info</h2>" 
    echo "<p>Function not yet implemented</p>" 

} # end of system_info 


show_uptime() 
{ 
    echo "<h2>System uptime</h2>" 
    echo "<pre>" 
    uptime 
    echo "</pre>" 

} # end of show_uptime 


drive_space() 
{ 
    echo "<h2>Filesystem space</h2>" 
    echo "<pre>" 
    df 
    echo "</pre>" 

} # end of drive_space 


home_space() 
{ 
    # Only the superuser can get this information 

    if [ "$(id -u)" = "0" ]; then 
     echo "<h2>Home directory space by user</h2>" 
     echo "<pre>" 
     echo "Bytes Directory" 
     du -s /home/* | sort -nr 
     echo "</pre>" 
    fi 

} # end of home_space 


write_page() 
{ 
    cat <<- _EOF_ 
    <html> 
     <head> 
     <title>$TITLE</title> 
     </head> 
     <body> 
     <h1>$TITLE</h1> 
     <p>$TIME_STAMP</p> 
     $(system_info) 
     $(show_uptime) 
     $(drive_space) 
     $(home_space) 
     </body> 
    </html> 
_EOF_ 

} 

usage() 
{ 
    echo "usage: sysinfo_page [[[-f file ] [-i]] | [-h]]" 
} 


##### Main 

interactive= 
filename=~/sysinfo_page.html 

while [ "$1" != "" ]; do 
    case $1 in 
     -f | --file)   shift 
           filename=$1 
           ;; 
     -i | --interactive) interactive=1 
           ;; 
     -h | --help)   usage 
           exit 
           ;; 
     *)      usage 
           exit 1 
    esac 
    shift 
done 


# Test code to verify command line processing 

if [ "$interactive" = "1" ]; then 
    echo "interactive is on" 
else 
    echo "interactive is off" 
fi 
echo "output file = $filename" 


# Write page (comment out until testing is complete) 

# write_page > $filename 

回答

0

[...]表示“可选”。 |的意思是“交替”。

所以[[[-f file ] [-i]] | [-h]]分成如下:

[ 
    [ 
    [-f file ] 
    [-i] 
    ] 
    | 
    [-h] 
] 

所有的参数都是可选的。如果你传递参数,你可以通过[-f file] [-i][-h]之一。

该约定的用法有点混乱,因为一组[]用于分组,因为它用于可选标记。这使得内部可选标记有点不合适,但仍然是必要的,因为单个参数仍然是可选的。