LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Closed Thread
  Search this Thread
Old 07-27-2012, 06:02 AM   #1
suboss87
Member
 
Registered: Dec 2010
Posts: 31

Rep: Reputation: 0
Red face 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
 
Old 07-27-2012, 06:20 AM   #2
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
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

Last edited by divyashree; 07-27-2012 at 06:32 AM.
 
1 members found this post helpful.
Old 07-27-2012, 06:26 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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"
 
1 members found this post helpful.
Old 07-27-2012, 06:45 AM   #4
roger_heslop
Member
 
Registered: Oct 2009
Location: Leander, TX
Distribution: Fedora 20
Posts: 97

Rep: Reputation: 35
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
 
1 members found this post helpful.
Old 07-27-2012, 07:10 AM   #5
roger_heslop
Member
 
Registered: Oct 2009
Location: Leander, TX
Distribution: Fedora 20
Posts: 97

Rep: Reputation: 35
#!/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
 
1 members found this post helpful.
Old 07-27-2012, 12:11 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
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
 
1 members found this post helpful.
Old 07-28-2012, 07:53 AM   #7
suboss87
Member
 
Registered: Dec 2010
Posts: 31

Original Poster
Rep: Reputation: 0
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..
 
Old 07-29-2012, 05:12 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
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
 
1 members found this post helpful.
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
writing a shell script icecubeflower Linux - Newbie 1 03-29-2009 01:16 PM
Help writing a shell script! smoothdogg00 Programming 5 04-26-2006 11:15 AM
shell script writing ust Linux - General 6 01-24-2005 01:06 PM
writing my first shell script speedhead34 Linux - Newbie 4 12-20-2004 12:50 AM
help writing shell script np complete Linux - Newbie 6 08-30-2004 09:43 PM


All times are GMT -5. The time now is 02:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration