【问题标题】:How to make a non interactive shell scripts [duplicate]如何制作非交互式shell脚本[重复]
【发布时间】:2014-07-16 14:01:39
【问题描述】:

我想制作一个非交互式的 shell 脚本,我可以在脚本执行开始时提供选项。

当用户提供不同的输入时,我可以硬编码要采取的各种操作。

例如:

下面应该对target.txt执行一些操作

user@/root>myPlannedScript -p targer.txt   

下面应该对target.txt执行一些其他操作

user@/root>myPlannedScript -a targer.txt  

例如:

cat 工具在给出不同选项时执行各种操作。我希望我的脚本是这样的:

:/root> cat --h  
Usage: cat [OPTION] [FILE]...  
Concatenate FILE(s), or standard input, to standard output.  

  -A, --show-all           equivalent to -vET  
  -b, --number-nonblank    number nonblank output lines  
  -e                       equivalent to -vE  
  -E, --show-ends          display $ at end of each line  
  -n, --number             number all output lines  
  -r, --reversible         use \ to make the output reversible, implies -v  
  -s, --squeeze-blank      never more than one single blank line  
  -t                       equivalent to -vT  
  -T, --show-tabs          display TAB characters as ^I  

【问题讨论】:

  • 你到底有什么问题?
  • 我有一个脚本,我使用用户交互(读取工具)来提供 target.txt,现在我想通过在一行中提供 target.txt 文件名来一次性执行此命令.

标签: linux bash shell scripting


【解决方案1】:

查询.sh

#!/bin/bash
if [ $# -eq 0 ]
then echo do one thing
else echo do other thing
fi

"query.sh" => 做一件事

"query.sh anythingYouPut" => 做其他事情 ;oP

但如果你真的想要每个动作的参数

#!/bin/bash
if [ -z "$1" ]
then
  echo do nothing
else
  if [ $1 -eq 1 ]
  then
    echo do one thing
  fi
  if [ $1 -eq 2 ]
  then
    echo do other thing
  fi
fi

"query.sh" => 什么都不做

"query.sh 1" => 做一件事

"query.sh 2" => 做其他事情

【讨论】:

    猜你喜欢
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    相关资源
    最近更新 更多