Some small questions about the id user script

hint:

“引用”一个命令的执行结果,要使用命令引用;比如: RESAULTS=`wc -l /etc/passwd | cut -d: -f1`;
使用一个命令的执行状态结果,要直接执行此命令,一定不能引用;比如: if id user1一句中的id命令就一定不能加引号;
如果想把一个命令的执行结果赋值给某变量,要使用命令引用,比如USERID=`id -u user1`;
如果想把一个命令的执行状态结果保存下来,并作为命令执行成功与否的判断条件,则需要先执行此命令,而后引用其状态结果,如

    id -u user1
    RETVAL=$?
    此句绝对不可以写为RETVAL=`id -u user1`;

When writing a script about adding a user, I encountered some problems when judging whether the user exists or not. The function implemented by this script is to judge whether a user exists or not. If it exists, it will be displayed, and if it does not exist, it will be added. It took me more than an hour for such a simple script. As a novice, I still summarize it so as not to make mistakes later.
According to the prompt at the beginning, the first script I wrote was like this

#!/bin/bash
name=user1
if [ id $name &> /dev/null ];then
    useradd $name &> /dev/null
    echo "user $name add finished"
else
    echo "user $name exit"
fi  

Because I don't need to add the single quotation mark when I want to get the result of the command execution status, I thought this script was fine at first. When the command is executed correctly, the return value is 0, and when it is wrong, it is a non-zero value. Then according to my knowledge of C language, if there is a 0 after the if, it means that the correct information of the non-execution state result will be displayed after the else, and the wrong information will be displayed after the then. But this result is horribly wrong! ! ! ! ! ! ! ! !

if [  ];then
    echo "A"
else
    echo "B"
fi

When I use this script to test, the else echo B will be executed only when [ ] is empty, that is, when there is nothing in it, no matter if [ ] is 0 or any other value, the statement after then will be executed. echo A. This proves a truth: zero does not mean no! , and just like the error script above, whether it is correct or incorrect, there is a return value, so the statement after then is executed.

So I changed the method to write:

#!/bin/bash

name=user1

id $name &> /dev/null
reval=$?
if [ $reval -eq 0 ];then
    echo "user $name exit"
else
    useradd $name &> /dev/null
    echo "user $name add finished"      
fi  

In this way, the prompt at the beginning is used. If you need to save the execution status result of the previous command, use a variable to save it.

But there is another way:

#!/bin/bash

name=user1

if  id $name &> /dev/null ;then
  echo "user $name exit"
else
    useradd $name &> /dev/null
    echo "user $name add finished"
fi  

This method does not need to add the conditional test to determine the symbol [ ]. This is very simple. If the user user exists, execute the statement after the then, and if the user user does not exist, execute the statement after the else.

Learning knowledge, especially computer computing knowledge, still needs to pay attention to practice, of course, the first thing is to persevere! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324831521&siteId=291194637