简体   繁体   English

Shell脚本中的OR条件-Unix

[英]OR condition in Shell Scripting - Unix

I declare three variables. 我声明三个变量。

$1=`ssh <server_1> cat /etc/passswd|cut -f -d:|grep -e $IID -e $EID`
$2=`ssh <server_2> cat /etc/shadow|cut -f -d:|grep -e $IID -e $EID`
$3=`ssh <server_3> cat /etc/passwd}|cut -f -d:|grep -i $CID`

The above three variables are created by taking ssh to servers and checking the presence of the IDs which I give as input. 通过将ssh带入服务器并检查是否存在我作为输入提供的ID,可以创建以上三个变量。 If the ID doesn't exist already, the the variable is going to be null. 如果ID还不存在,则变量将为null。

Now, how do I verify if all the three variables are null. 现在,如何验证所有三个变量是否为空。 I wanted to use the OR condition specified within an IF. 我想使用IF中指定的OR条件。

I tried, 我试过了,

if [ -s "$1" -o  -s "$2" -o  -s "$3"];then
echo -$1 $2 $3 "already exist(s)"

It didnt work. 它没有工作。 Please advise. 请指教。

PS: I have just begun my career in Unix and correct me If am wrong anywhere. PS:我刚刚开始在Unix上的职业,如果在任何地方出错,我都可以纠正我。

Several points. 几点。

When you assign to a variable, don't use the dollar sign: 在分配变量时,请勿使用美元符号:

foo=xxx

Variables $1 , $2 etc are already used for your command line arguments. 变量$1$2等已用于命令行参数。 Pick other names. 选择其他名称。 But not $4 please. 但是请不要花$4 :-) :-)

When you specify a command for ssh, and it has arguments, it has to be quoted, because the command needs to be a single argument for ssh. 为ssh指定命令时,如果该命令具有参数,则必须将其引起引用,因为该命令必须是ssh的单个参数。 In your case use double quotes, as you want variable expansion for $IID etc. 在您的情况下,请使用双引号,因为您需要$IID等的变量扩展。

Most Unix utils are able to open input files themselves, so you don't need to start your pipeline with cat . 大多数Unix utils都可以自己打开输入文件,因此您不需要使用cat启动管道。

foo=`ssh <server_1> "cut -f -d: /etc/passwd | grep -e $IID -e $EID"`

Or something like that. 或类似的东西。

It was a typo in my question. 这是我的错字。 I had actually declared it as, 我实际上已经宣布为

1=`ssh <server_1> cat /etc/passswd|cut -f -d:|grep -e $IID -e $EID` 
2=`ssh <server_2> cat /etc/shadow|cut -f -d:|grep -e $IID -e $EID` and so on. 

And I tried it as , 我尝试过,

if [ -s "$1" -o  -s "$2" -o  -s "$3"];then
echo -e $1 $2 $3 "already exist(s)"

Since I had to Deliver my script today, I used the conventional method of, 由于我今天必须提交脚本,因此我使用了常规方法,

ssh <server_1> "cat /etc/passswd|cut -f -d:|grep -e $IID -e $EID" > file1
ssh <server_2> "cat /etc/shadow|cut -f -d:|grep -e $IID -e $EID" > file2
ssh <server_3> "cat /etc/passwd|cut -f -d:|grep -ix $CID" > file3

if [ -s file1 -o -s file2 -o -s file3]; then
for i in `cat file1 file2 file3`
do
echo $i "already exists"
done
else

And I have now learnt from my first post, that -s to ensure that a file is not empty and -z is to ensure string is empty. 现在我从第一篇博文中了解到-s确保文件不为空,-z确保字符串为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM