Isn't a shell found on a beach? Need help nesting if's or loops.
As of a week ago i thought a shell was somthing found on a beach.
I'm a virgin when it comes to scripting and i'm having a really bad time here.
What i need to do is prompt for a group number
grep the /etc/groups to get the GID and name
if it exists i want to prompt the user for comfirmation to add user to the group (the other part of the script...thats working) while dispalying the group name and id.
if not i want to promt the user to make a group with name and id supplied by user input via promts.
this is what i have so far.
Sorry for being such a noob.
Code:
#!/bin/bashechoecho "User ID?"echoread INPUTTEMPIN=$INPUT;TEMPTN=`grep ":${TEMPIN}:" /etc/group |cut -d: -f3`;TEMPST=`grep ":${TEMPIN}:" /etc/group |cut -d: -f1`;echo "True number ${TEMPTN}"echo "True String ${TEMPST}"echo "Input ${TEMPIN}"if [ $TEMPNT==$TEMPIN ]; then echo echo "GID : ${TEMPTN}" echo "Group name : ${TEMPST}" echo echo "Already exists, do you want to use this group" echo echo "<y> or <n>" read INPUT echo if [ ! $INPUT = "n" ]; then GROUP_ID=${TEMPIN}; fifiif [ ! $TEMPNT==1$TEMPIN ]; then echo echo "Group ID ${TEMPIN} does not exist" echo "would you like to create it?" echo echo "<y> or <n>" echo read INPUT echo if [ ! $INPUT = "n" ]; then echo echo "Enter group name" read NAME echo GRP=$NAME; GID=$TEMPIN; else echo "Setup cannot contine without a specified group!" exit fifi
As of a week ago i thought a shell was somthing found on a beach....
Sorry for being such a noob.
That you've managed to get this far in a week is very much not noob'ish, good progress
What you have looks to be about right, but as vbe says, you've got a typo in there. It looks like you've also introduced a '1' character in that same line, was that deliberate?
As an aside, when comparing two variables in shell scripts, it's a lot safer to use the form:
Code:
if [ "$TEMPTN" == "$TEMPIN" ]
then
do_stuff
fi
That way, if one of the variables is zero length, you won't have the script fail with a syntax error.
I don't think you need to do all that checking that the gid they provide matches the group found by grep...
I'd also suggest cleaning up some of your variable assignments, things like reading to INPUT, then immediately assigning it to TEMPIN is somewhat of a waste really.
My go at the problem:
Code:
#!/bin/sh
# Using sh rather than bash as you don't need the extra functions in bash when sh will do the job :)
# Will either exit with a non-zero (error) return code or will populate the GID and GRP variables with a group and groupname.
# If GRP is zero length, the group already exists, if it contains text, it must first be created.
GRP="" # Make sure it's not still set from a previous run
echo Group ID?"
read GID
groupname=`grep ":$gidin: /etc/group | cut -d ':' -f 1`
if [ -n "$groupname" ]
then
# The group already exists
echo "$groupname (GID: $GID already exists."
echo "Do you want to use this group? (y/n)"
read input
if [ ! "$input" == "y" ]
then
echo "Exiting..."
exit 1
fi
else
# The group does not yet exist
echo "Group id $GID does not already exist."
echo "Would you like to create it? (y/n)"
read input
if [ "$input" == "y" ]
then
echo "Enter group name"
read GRP
else
echo "Exiting..."
exit 1
fi
fi
(Untested)
For the next bit of logic (you say you've already got the group create / username add bit working?) I'd do this:
Code:
# Rest of code here
if [ -z "$GRP" ]
then
# create the group
fi
# Code to add username to groupname $GRP, gid $GID here
I have a script to start/stop/restart the tomcat application. When we run the script first time i.e stop/start it set all env variables(DISTRIB_ID,NAME,TOMCAT_CFG,....etc),but when we restart the tomcat it is running in the same shell.....I need to set the variables when i restart the tomcat(in the... (1 Reply)
Hi I have the following Korn script having multiple for loops.
#!/bin/ksh
EXPECT=/usr/local/bin/expect
exp_internal
for d in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 i22 23 24 25 26; do
for i in 01 02 03 04 05 06 07 ; do
for h in 00 01 02 03 04 05 06 07 08 09 10 11 12... (2 Replies)
Hi Guys-
I'm trying to write a script which takes date as input (mm.yy.dd) and search in the current file. If pattern doesn't exist it will then look in a backup directory and so on.
being a newb i'm unable to loop over to the backup directory. hoping for some ideas, i've highlighted the... (1 Reply)
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
"Create a shell script that changes the selected word to another word in all files from selected archive. The... (1 Reply)
Hi,
#!/bin/ksh
$v=""
for ((i = 1 ; i <= 5 ; i++ ))
do
v="THerrFile_$i.err";
grep -i "$i:Error" $v >>oraerror_output.txt
done
My requirement is to dynamically create variable like
THerrFile_1.err,THerrFile_2.err etc.
where my grep needs... (5 Replies)
Hello All,
I have another Korn shell question. I am writing a script that will ftp a file from my production database to my test database.
To this, I have to construct a loop that checks a specified folder for a file.
If the file exists, I want it execute the ftp protocol and then exit. ... (2 Replies)
Hi!
Can anyone tell me what went wrong in my shell script?
for dt_val in `cut -f 1 -d '|' /prod/ods/satyaki/sqlldr/grp.dat`
do
echo $dt_val
done
And, the output is -
23
39 (7 Replies)
Hi guys,
i need to script the sending of multiple files through one ftp session. I have done this so far:
----------------------------------------------------------------
#!/bin/sh
USER=myuser
PASSWD=mypass
filenum=$1
x=0
ftp -n 159.167.95.199 <<SCRIPT
user $USER $PASSWD
binary
while ... (4 Replies)