Isn't a shell found on a beach? Need help nesting if's or loops.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Isn't a shell found on a beach? Need help nesting if's or loops.
# 1  
Old 09-01-2008
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/bash
 
echo
echo "User ID?"
echo
read INPUT
 
TEMPIN=$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};
                 fi
fi
 
if [ ! $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
           fi
fi

# 2  
Old 09-01-2008
Whats $TEMPNT?
Youve initialized TEMPTN and TEMPST and then?
How do we guess [ $TEMPNT==$TEMPIN ]?
# 3  
Old 09-01-2008
Java

Quote:
Originally Posted by switchkill
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 Smilie

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

# 4  
Old 09-01-2008
I can't thank you enough for the help Dragon.

You have saved my sanity.

Cheers.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unset variables in shell when it running two different loops

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)
Discussion started by: praveen265
1 Replies

2. Shell Programming and Scripting

korn shell for loops with expect issue

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)
Discussion started by: cic
2 Replies

3. Shell Programming and Scripting

help with multiple loops in shell script

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)
Discussion started by: Irishboy24
1 Replies

4. Homework & Coursework Questions

Shell-script loops beginner

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)
Discussion started by: oibanuelos
1 Replies

5. Shell Programming and Scripting

Problem in loops in shell scripting

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)
Discussion started by: sudhir_83k
5 Replies

6. Shell Programming and Scripting

Korn Shell programming (FTP, LOOPS, GREP)

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)
Discussion started by: jonesdk5
2 Replies

7. Shell Programming and Scripting

shell script, why isn't if printing message?

Why isn't printing message? 1 #!/bin/sh 2 3 something(){ 4 echo "Inside something" 5 echo $1 $2 6 } 7 val=$(something "Hello " "world") But it prints. 1 #!/bin/sh 2 3 something(){ 4 echo "Inside something" 5 echo $1 $2 6 } 7... (4 Replies)
Discussion started by: cola
4 Replies

8. Shell Programming and Scripting

Output in my shell isn't showing properly.

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)
Discussion started by: satyakide
7 Replies

9. Shell Programming and Scripting

Loops within ftp shell session

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)
Discussion started by: zaff
4 Replies
Login or Register to Ask a Question