【发布时间】:2018-10-10 08:49:45
【问题描述】:
我必须编写一个脚本,该脚本将根据从命令行传递的参数执行各种任务。脚本的名称是“safeDel.sh” 到目前为止我所拥有的如下:
#!/bin/bash
arg=$1
r_file=$2
if [$arg == '-l']
then
#List all files in trash can
echo '$arg'
elif [$arg == '-r']
then
#recover r_file
echo '$arg'
elif [$arg == '-d']
then
#Delete (interactively?) the contents of trash directory
echo '$arg'
elif [$arg == '-t']
then
#Display total usage in bytes of trash directory
echo '$arg'
fi
在这个阶段,我只是尝试使用 if/else 语句来打印出适当的参数。但是,如果我输入 './safeDel.sh -r',输出是:
./safeDel.sh: 第 6 行 [-r 命令未找到]
./safeDel.sh: 第 11 行 [-r 命令未找到]
./safeDel.sh: 第 16 行 [-r 命令未找到]
./safeDel.sh: 第 21 行 [-r 命令未找到]
修改代码的正确方法是什么,以便我可以让脚本根据传递的参数执行某些任务?
【问题讨论】:
-
在
[之后和]之前添加空格。还要加倍你的变量"$arg"。最后去shellcheck.net验证你的文件 -
空格已修复,谢谢@oliv
标签: linux bash terminal arguments