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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-27-2012, 06:02 AM
|
#1
|
Member
Registered: Dec 2010
Posts: 31
Rep:
|
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
|
|
|
07-27-2012, 06:20 AM
|
#2
|
Senior Member
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386
Rep: 
|
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.
|
07-27-2012, 06:26 AM
|
#3
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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.
|
07-27-2012, 06:45 AM
|
#4
|
Member
Registered: Oct 2009
Location: Leander, TX
Distribution: Fedora 20
Posts: 97
Rep:
|
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.
|
07-27-2012, 07:10 AM
|
#5
|
Member
Registered: Oct 2009
Location: Leander, TX
Distribution: Fedora 20
Posts: 97
Rep:
|
#!/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.
|
07-27-2012, 12:11 PM
|
#6
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
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.
|
07-28-2012, 07:53 AM
|
#7
|
Member
Registered: Dec 2010
Posts: 31
Original Poster
Rep:
|
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.. 
|
|
|
All times are GMT -5. The time now is 03:18 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|