BASH第八课作业

来源:互联网 发布:ios app 访问数据库 编辑:程序博客网 时间:2024/06/01 07:12
1写一个脚本,完成如下功能(使用函数):
1、脚本使用格式:
mkscript.sh [-D|--description "script description"] [-A|--author "script author"]   文件名
2、如果文件事先不存在,则创建;且前几行内容如下所示:
#!/bin/bash
# Description: script description
# Author: script author
#
3、如果文件事先存在,但不空,且第一行不是“#!/bin/bash”,则提示语法错误并退出;如果第一行是“#!/bin/bash”,则使用vim打开脚本;把光标直接定位至最后一行
4、打开脚本后关闭时判断脚本是否有语法错误
如果有,提示输入y继续编辑,输入n放弃并退出;

如果没有,则给此文件以执行权限;

#!/bin/bashexec 2>> /dev/nullwhile getopts :D:A: opt; do    case $opt in        D)        DES="$OPTARG"        ;;        A)        AUT="$OPTARG"        ;;        ?)         echo "Invalid param" ;;    esacdonedir=/tmp/tkpif [ ! -d $dir ];thenmkdir $dirfiNUMBER=$#declare -a argsi=1while [ $i -le $NUMBER ]do        args[$i]=$1                                 #echo ${args[$i]}        ((i++))                               shift                    donefile=${args[$NUMBER]}file_path=$dir/$filefunction touch_file(){touch $file_pathecho "#!/bin/bash" >> $file_path echo "# Description: $DES" >> $file_pathecho "# Author: $AUT" >> $file_pathecho "#" >> $file_path}function syntax_file(){bash -n $file_pathif [ $? -eq 0 ];thenchmod 755 $file_pathecho "script is correct,you can run it now"elseecho "the file has syntax error"read -p "Y/N,Y to continue edit the script,N to ignore the error and exit" flagif [ $flag == "Y" -o $flag == "y" ];thenvim + $file_pathsync_fileelif [ $flag == "N" -o $flag == "n" ];thenecho "quit script"exit 2elseecho "Invalid parameter,quit script"exit 1fifi}if [ -f $file_path ];thenhead -1 $file_path | grep "^\#\!\/bin\/bash$" >> /dev/nullif [ $? -ne 0 ];thenecho "Syntax error!!!"exit 1elsevi + $file_pathsyntax_filefielif [ ! -f $file_path ];thentouch_filefi


0 0
原创粉丝点击