linux判断字符串命令行,bash – 将命令行参数与字符串进行比较

这是我的代码:

#!/bin/bash

if [ "$#" -ne 2 ] ; then

echo "$0: exactly 2 arguments expected"

exit 3

fi

if [$1 != "file" -a $1 != 'dir'] ; then

echo "$0: first argument must be string "file" or "dir""

exit 1

elif [-e $2 -a -r $2]; then

if ["$1" = "file" -a -f $2] ; then

echo YES

elif ["$1" = "dir" -a -d $2] ; then

echo YES

else

echo NO

fi

exit 0

else

echo "$0: $2 is not a readable entry"

exit 2

fi

如果我运行./lab4文件filename1,它将检查第一个参数是字符串“file”还是“dir”,如果第一个参数是“file”,filename1是一个文件,它将打印yes。相同的东西对于目录。

它不识别$ 1和$ 2。代码将输出:

./lab04Q2: line 7: [file: command not found

./lab04Q2: line 10: [-e: command not found

即使我在运行程序时放置了2个参数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值