【问题标题】:Accepting multiple arguments through sh script [UNIX]通过 sh 脚本接受多个参数 [UNIX]
【发布时间】:2013-04-10 14:49:23
【问题描述】:

我在这里尝试做的是允许用户输入多个参数(文件名)并将它们发送到我创建的回收站 sh 脚本称为“sh safe_rm”

所以我会这样做:

$] sh safe_rm testFile2 testFile 3

我需要两个文件都可以通过,有什么想法吗?

file=$1

if [ $# -eq 0 ]
then
   echo "You have not entered a file"
   exit
elif [ -d $file ]
then
   echo "Your file is a directory"
   exit
elif [ -e $file ]
then
   sendToBin
else
   echo "Your file $file does not exist"
   exit


  fi

【问题讨论】:

标签: bash unix arguments sh


【解决方案1】:

试试这样的:

sh safe_rm testFile2 testFile 3

for var in "$@"
do
    echo "$var"
done

这将打印:

testFile2
testFile
3

See this post for more info

【讨论】:

  • +1。 @claybanks,请注意在任何地方都必须使用双引号。
猜你喜欢
  • 2011-03-24
  • 1970-01-01
  • 2012-05-21
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 2012-01-12
  • 1970-01-01
  • 2014-10-27
相关资源
最近更新 更多