需求如下:

写一个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. Redhat终端中文乱码解决

    文件中的中文以及命令反馈的中文能够正常显示,但是在终端中用ls等命令查看文件时会出现乱码. 我在i18n文件中加了下面两行内容(本来只有第一行),后来就能正常显示了.

  2. vue数据变动监测

    原文链接:https://blog.csdn.net/man_tutu/article/details/72148362 对象: 不能监测到: var vm = new Vue({ data:{ a: ...

  3. 编译原理中DFA最小化

    关于编译原理最小化的操作,专业术语请移步至:http://www.360doc.com/content/18/0601/21/11962419_758841916.shtml 这里只是记录一下个人的理 ...

  4. JQurey 添加和删除元素

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> ...

  5. MySQL优化查询 5.7版本

    1. 变更参数 : query_cache_type 如果何配置查询缓存: query_cache_type 这个系统变量控制着查询缓存工能的开启的关闭.query_cache_type=0时表示关闭 ...

  6. 从零开始,搭建 AI 音箱 Alexa 语音服务

    https://blog.csdn.net/gitchat/article/details/78869367

  7. HTML5地理定位API在chrome中不能正常使用

    navigator.geolocation.getCurrentPosition在chrome中不能正常使用. 经测试发现,FQ后就能正常使用,估计是因为chrome 对这个API的实现使用了goog ...

  8. cowboy源码分析(三)

    上接 cowboy源码分析(二) 我们接着分析cowboy_protocol.erl的request/7模块 -module(cowboy_protocol). %% API.-export([sta ...

  9. 在chrome上隐藏video的option按钮

    隐藏方法: video::-webkit-media-controls{ overflow:hidden !important;}video::-webkit-media-controls-enclo ...

  10. 交换机telnet配置

    新开箱交换机开机配置Telnet需要三个步骤: 1.开启telnet是能:系统视图模式下输入命令: telnet server enable #开启telnet功能# 2.Telnet创建账号:aaa ...