LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   [SOLVED]Basic If statments problem (https://www.linuxquestions.org/questions/linux-newbie-8/%5Bsolved%5Dbasic-if-statments-problem-4175439093/)

darkkatana 11-28-2012 04:26 AM

[SOLVED]Basic If statments problem
 
Hi, im trying to create my first script for an Administrator to install users in debain(Ive noticed differences between debian and Opensuse in scripting/commands)and I'm haing having problems with the if statment. so far this is what I have written:

Code:

echo "Would you like to create a new user?(yes/no)"
read input
continue =$(yes)
if [ "$input" -ge "$continue" ]
then
echo "Please enter new user name"
                read UN
                useradd -m $UN
        echo "Please enter a password for $UN"
                passwd $UN
        echo "User '$UN' has been created"
fi
echo "Opperation cancelled"


but when i run the script I recieve this out put:

Code:

Would you like to create a new user?(yes/no)
yes

and then nothign after that, not even the treminal chevrons...

so basically im a little lost on what to do at the moment and help would be much apprecieated. also if there are any good online guides could somewone point them out please.
Thanks for reading.

Snark1994 11-28-2012 04:38 AM

Try running the "yes" command in the terminal... That's why it doesn't terminate. You wanted:

Code:

continue="yes"
(and your if condition should probably be
Code:

if [ "$input" == "$continue" ]
)

Hope this helps,

shivaa 11-28-2012 04:48 AM

Problem is with password line as you cannot supply password in advance in script itself, plus there's little syntax error. I tried to correct it, so once try it:
PHP Code:

echo "Would you like to create a new user?(yes/no)"
read input
if [ $input -eq "yes" ];
then
echo -"Please enter new user name: "
read UN
useradd 
-m $UN
passwd $UN  
## No need to do echo, but it will ask you to enter password
echo "User $UN has been created."
elif $input -eq "no" ]; then
echo "Opperation cancelled."
fi
fi 

Or you can do it in one more way as:
PHP Code:

case $choice in
yes
) echo -"Please enter new user name: "
read UN
passwd $UN
;;
no) echo "Opperation cancelled."
exit 0;;
*) echo 
"Enter a correct choice (yes/no):"
$(basename $0


darkkatana 11-28-2012 05:55 AM

Quote:

Originally Posted by Snark1994 (Post 4838622)
Try running the "yes" command in the terminal... That's why it doesn't terminate. You wanted:

Code:

continue="yes"
(and your if condition should probably be
Code:

if [ "$input" == "$continue" ]
)

Hope this helps,

thanks this helpped alot(it's working now)
and thanks shiva for pointing out those errors.


All times are GMT -5. The time now is 12:47 AM.