需求如下:

写一个shell脚本,检查指定的shell脚本是否有语法错误,若有错误,首先显示错误信息,然后提示用户输入q或Q退出脚本,

输入其他内容则直接用vim打开该shell脚本。

参考解答如下

  • 方法1
#!/bin/bash

cmd="/bin/bash"
ed="/usr/bin/vim" if [ $# -ne 1 ];then
echo "USAGE:$0 script_name"
exit 1
fi $cmd -n $1
if [ $? -ne 0 ];then
read -p "Please enter Q/q to exit, or other to edit it by vim."
if [ "$REPLY" = "q" -o "$REPLY" = "Q" ];then
exit 0
else
$ed $1
fi
else
echo "The scipt is OK."
fi
  • 方法2
#!/bin/bash

cmd="/bin/bash"
ed="/usr/bin/vim" if [ $# -ne 1 ];then
echo "USAGE:$0 script_name"
exit 1
fi $cmd -n $1 2>/tmp/err
if [ $? -eq 0 ];then
echo "The script is OK."
else
cat /tmp/err
read -p "Please enter Q/q to exit, or other to edit it by vim." n
if [ -z $n ];then
$ed $1
exit
fi
if [ "$n" = "q" -o "$n" = "Q" ];then
exit
else
$ed $1
exit
fi
fi

注意: bash -n选项只检测语法错误。

shell练习题2的更多相关文章

  1. shell 脚本之 shell 练习题汇总

    整理了一些 shell 相关的练习题,记录到这里. 1. 请按照这样的日期格式 xxxx-xx-xx 每日生成一个文件,例如:今天生成的文件为 2013-09-23.log, 并且把磁盘的使用情况写到 ...

  2. shell练习题5

    需求如下: 用shell实现,把一个文件文档中只有一个数字的行给打印出来.(以/password文件为例,自行修改) 参考解答如下 方法1 #!/bin/bash file_name=passwd n ...

  3. shell练习题4

    需求如下: 系统logrotate工具,可以完成日志切割.归档.写一个shell脚本实现类似功能. 举例:假如服务的输出日志是1.log,要求每天归档一个,1.log第二天就变成1.log.1, 第三 ...

  4. shell练习题1

    需求如下: 写一个shell脚本,把10.0.1.0/24网段在线的ip列出来. 参考解答如下 方法1 #!/bin/bash ip="10.0.1." for i in $(se ...

  5. shell练习题

    一.编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息. 原文代码为:         Shell   1 2 3 4 5 6 7 8 9 10 #!/bin/bash ...

  6. shell 练习题

    1.编写脚本/bin/per.sh,判断当前用户对指定参数文件,是否不可读并且不可写 read -p "Please Input A File: " file if [ ! -e ...

  7. shell 练习题1

    1.实现每次打开一个xx.sh时,自动添加注释信息 [root@chengyinwu ~]# cat .vimrc set ignorecase set nu set autoindent autoc ...

  8. shell练习题7

    需求如下: 输入一串随机数字,然后按千分位输出. 例如:输入随机数字为"123456789",输出为123,456,789 参考解答如下 -方法1 [root@lanquark s ...

  9. shell练习题6

    需求如下: 有日志access.log,部分内容如下: 127.0.0.1 - - [03/Jul/2018:00:00:01 +0800] "GET / HTTP/1.1" 20 ...

随机推荐

  1. IOS Xcode编译项目-报错“ld: library not found for -XX”

    一般是因为导入新项目的时候报错的.原因是引入的依赖库的问题.重新执行pod install应该可以解决.不过,有时候如果重新执行pod install无法执行,可以采用以下方法: 在终端中cd到项目所 ...

  2. linux中centros6.7安装php5.6,httpd-2.2.19(web产品化)遇到的问题总结

    前段时间在公司实习,web系统产品化的过程踩了很多坑,在这边总结一下,由于对linux不是很懂,全是自己一步步一个一个问题解决的 1,查看系统中是否安装apache,php,mysql环境 Apach ...

  3. 论文阅读-attention-is-all-you-need

    1结构介绍 是一个seq2seq的任务模型,将输入的时间序列转化为输出的时间序列. 有encoder和decoder两个模块,分别用于编码和解码,结合时是将编码的最后一个输出 当做 解码的第一个模块的 ...

  4. Phpstorm-远程连接服务器实时编辑代码

    一,配置远程服务器FTP 1.打开Phpstorm >>> 选择 2.选择 3.输入项目名称(此项目名称不会同步到服务器,只是在本地电脑上的项目名称) 4.将编辑器设置为实时自动同步 ...

  5. NetSec2019 20165327 Exp2 后门原理与实践

    NetSec2019 20165327 Exp2 后门原理与实践 快速找到重点: (1)使用netcat获取主机操作Shell,cron启动 (0.5分) (2)使用socat获取主机操作Shell, ...

  6. Exception in thread "main" SettingsException[Failed to load settings from [elasticsearch.yml]]; nested: ElasticsearchParseException[malformed, expected end of settings but encountered additional conte

    D:\elasticsearch\elasticsearch-2.4.0\bin>elasticsearchException in thread "main" Settin ...

  7. eclipse运行项目,tomcat报错:Exception in thread :http-bio-8080-exec-4

    eclipse运行项目,tomcat报错:Exception in thread :http-bio-8080-exec-4 转自 https://www.cnblogs.com/yby-blogs/ ...

  8. D - Dice Game (BFS)

    A dice is a small cube, with each side having a different number of spots on it, ranging from 1 to 6 ...

  9. Mac OS X 操作系统下IntelliJ IDEA激活码(Activation code)破解

    Mac OS X 操作系统(版本:10.13.6)下载并安装IntelliJ IDEA: Version: 2018.2.4 Download IntelliJ IDEA 下一步对IntelliJ I ...

  10. Response.End ,Response.Redirect、Server.Transfer 引发 “正在中止线程”异常的问题

    google后得知:Response.End 方法终止页的执行,并将此执行切换到应用程序的事件管线中的 Application_EndRequest 事件,同时抛出ThreadAbortExcepti ...