LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need a help for writing shell script (https://www.linuxquestions.org/questions/linux-newbie-8/need-a-help-for-writing-shell-script-4175418923/)

suboss87 07-27-2012 06:02 AM

need a help for writing shell script
 
Hi, I need to write a shell scrit to create two users with different home directory path and also i need to make the process of script in an log file..Hereby i do attached the script which i had written my own. But the script not working properly please guide me...

#!/bin/bash

#script to change the sever configuration

usr1=gp
usr2=pr
pass1=gp123
pass2=pr123

su - root


/bin/mkdir /102/gp
/usr/sbin/useradd -d /102/gp -m /sbin/nologin -g 500 -u 525 $usr1
echo $usr1 | passwd --stdin $pass1
chmod 770 /102/gp
if /etc/passwd | grep gp
echo "gp created sucessfully" >> /root/Desktop/log
else
echo "gp failed to create" >> /root/Desktop/log
fi



/bin/mkdir /102/pr
/usr/sbin/useradd -d /102/pr -m /sbin/nologin -g 500 -u 550 $usr2
echo $usr2 | passwd --stdin $pass2
chmod -R 770 /102/prov
chown -R prov:cms /102/pr/
if /etc/passwd | grep pr
echo "pr user created sucessfully" >> /root/Desktop/log
else
echo " pr user failed to create" >> /root/Desktop/log
fi

divyashree 07-27-2012 06:20 AM

change your if condition and grep like this:

Quote:

grep gp /etc/passwd
if [ $? -eq 0 ]
then
echo "gp created sucessfully" >> /root/Desktop/log
else
echo "gp failed to create" >> /root/Desktop/log
fi

pixellany 07-27-2012 06:26 AM

Please put your code into tags (in advanced edit mode, select the code, and then click on the ""#")

In this case, I see several errors. The best way to proceed will be to first try the individual commands and make sure you know how they work---then put them together in a script.

Some errors:

1. The "if" construct needs to look like this:
Code:

if <something>, then
    <commands>
fi

2. What is this supposed to do?: "if /etc/passwd | grep gp"
/etc/passwd is not a command---if you want to read from that file, then you need a command that reads the file---eg do this in a terminal:
"grep "gp" /etc/passwd"

roger_heslop 07-27-2012 06:45 AM

When you use mkdir in a script, it's a good idea to use the -p switch, (mkdir -p). That ensures all of the parent directories get created if they do not exist.

When using the -g for groupadd, the group needs to already exist, so consider creating it before the useradd command.
(via "groupadd 'groupname' -g 500")

Remember the ";" in your if/then statements

I'm testing the script now on virtual machine, I'll let you know once I get it working. Reply if you beat me to it :)

roger_heslop 07-27-2012 07:10 AM

#!/bin/bash
#script to change the sever configuration

usr1=gp
usr2=pr
pass1=gp123
pass2=pr123
group=groupname #change this to whatever you want

/bin/mkdir /102
/usr/sbin/groupadd groupname -g 501
/usr/sbin/useradd -d /102/gp -m -s /sbin/nologin -g 500 -u 525 $usr1
passwd $usr1 << EOF # I changed this a bit, but it works
$pass1
$pass1
EOF
cp /etc/skel/. /102/$usr1 #Makes sure these are copied, they will be skipped if the directory exists
chown -R $usr1:$group /102/$usr1
chmod -R 770 /102/gp
cat /etc/passwd | grep $usr1 && echo "gp created sucessfully" >> /root/log.txt || echo "gp failed to create" >> /root/log.txt



/bin/mkdir -p /102/$usr2
/usr/sbin/useradd -d /102/pr -m -s /sbin/nologin -g 500 -u 550 $usr2
passwd $usr2 << EOF
$pass2
$pass2
EOF
cp /etc/skel/. /102/$usr2
chown -R $usr2:$group /102/$usr2
chmod -R 770 /102/$usr2
cat /etc/passwd | grep $usr2 && echo "gp created sucessfully" >> /root/log.txt || echo "gp failed to create" >> /root/log.txt

David the H. 07-27-2012 12:11 PM

Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.

Code:

"grep gp"
This will find that two-letter string if it exists anywhere in the text. There's a very good chance of hitting false positives using it. You really need to use a regex that narrows down the match. Use "grep '^gp:'", perhaps.


Code:

cat /etc/passwd | grep $usr1 && echo "gp created sucessfully" >> /root/log.txt || echo "gp failed to create" >> /root/log.txt
This starts with a Useless Use Of Cat, and continues by hard-coding the username in the output.

Also, variable expansions should never be left unquoted (although it doesn't matter much here since the usernames have no spaces or other reserved characters, it's still sloppy practice).

A better design for the script would probably be to not hard-code the usernames, but to set them up as inputs to the script, and loop over them. Or at least set them up in an array or something.

Code:

unames=( gp pr )
upass=( gp123 pr123 )


for i in "${!unames[@]}"; do

        #a sample command:
        echo "The user ${unames[i]} has the password ${upass[i]}"

done


suboss87 07-28-2012 07:53 AM

Dear All,
Personally I thank all for your valuable reply. Now am really got an clear idea about my script. Yet I have to add some feature to my script. I need your support for the same. Hereby I do attached the list of features.


Features:


1: If all my servers have only normal user as default login, How to make a switching of normal user account to root account(including passwd of root in script) at the first step of script.

2: If I am having n-number of servers, How to make my script to push remotely by "ssh or scp" from one server to all other servers by adding their ip or host name within the script.


THANKS IN ADVANCE..:)
Waiting for reply..:)

chrism01 07-29-2012 05:12 PM

1. Don't do that (ie never put a passwd in a script).
in this case, enable the target user via the sudo cmds & sudoers file to be able to create users.
http://linux.die.net/man/8/sudo
http://linux.die.net/man/5/sudoers
http://linux.die.net/man/8/visudo

2. use ssh-auth-keys
http://www.linuxtopia.org/online_boo...erate-keypairs


All times are GMT -5. The time now is 05:37 PM.