LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-13-2012, 05:53 PM   #1
itsjustme
Senior Member
 
Registered: Mar 2003
Location: Earth
Distribution: Slackware, Ubuntu, Smoothwall
Posts: 1,571

Rep: Reputation: 45
bash script help


I have a friend who is taking an online course. She is doing very well, but is having trouble with bash scriptng. I have no knowledge of bash scripting syntax so I thought I would pass this along and see if anyone can point us in the right direction.

All this thing does is ask for input and then stops.

Code:
#!/bin/bash
#Individual Project 5 for UNIX CS126
#Create a Menu
echo "Choose an option
a. Create Users
b. Create Groups
c. Create Directories
d. Create Links
e. Set File Permissions
f. exit"
read RESPONSE
if [“$RESPONSE” = "a"]; then
#Create Users
echo "What is your NAME?"
read NAME
if [“$NAME” -!= "Tiara"]; then
echo "Your name is $NAME"
useradd -c $NAME
else
echo "That's my name too."
fi
#Create Groups
if [“$RESPONSE” = "b"]; then
echo “What is the name of the GROUP?”
read GROUP
if test -g $GROUP; then
echo “That group name exists, please try again.”
else
echo “The name of the group is $GROUP.”
groupadd $GROUP
fi
fi
#Create Directories
if [“$RESPONSE” = "c"]; then
echo “What is the name of the DIRECTORY1?”
read DIRECTORY1
if test -d $DIRECTORY1; then
echo “That directory name exists, please try again.”
else
echo “The name of the directory is $DIRECTORY1.”
mkdir $DIRECTORY1
ls -l
echo “What is the name of the DIRECTORY2?”
read DIRECTORY2
if test -d $DIRECTORY2; then
echo “That directory name exists, please try again.”
else
echo “The name of the directory is $DIRECTORY2.”
mkdir $DIRECTORY2
ls -l
echo “What is the name of the DIRECTORY3?”
read DIRECTORY3
if test -d $DIRECTORY3; then
echo “That directory name exists, please try again.”
else
echo “The name of the directory is $DIRECTORY3.”
mkdir $DIRECTORY3
ls -l
echo “What is the name of the DIRECTORY4?”
read DIRECTORY4
if test -d $DIRECTORY4; then
echo “That directory name exists, please try again.”
else
echo “The name of the directory is $DIRECTORY4.”
mkdir $DIRECTORY4
ls -l
fi
fi
fi
fi
fi
#Create Links
if [“$RESPONSE” = "d"]; then
echo “Links all directories together.”
cd $DIRECTORY1
ls $DIRECTORY2_ln $DIRECTORY3_ln $DIRECTORY4_ln
ls
fi
#File Permissions
if [“$RESPONSE” = "e"]; then
echo “ This will modify permissions.”
usermod -g $GROUP $NAME
groups $NAME
chown $NAME $DIRECTORY1
ls -l
chmod u+x, g+w $DIRECTORY1
chmod u+x, g-r, o-r $DIRECTORY2
chmod u+x, g-r, o-r $DIRECTORY3
chmod u+x, g-r, o-r $DIRECTORY4
ls -l
fi
fi
exit 0
Like I said I don't know anythng about that 'read' or the syntax of the if statements. But when I run this, I get the menu, type in 'a' and then it just stops.

Any help?

Thanks...
 
Old 11-13-2012, 06:59 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,991
Blog Entries: 54

Rep: Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743Reputation: 2743
Change the first line from
Code:
#!/bin/bash
to
Code:
#!/bin/bash -vxe
That sets debug mode and makes it easier to see where things go wrong. For instance there should be at least a single space between quoted variables and the square bracket, you may reverse meaning by placing an exclamation sign in front of the equal sign -!= but adding a minus in front of that isn't valid and generally speaking it would be much, much easier to use a "case" statement instead of (nested!!!) if-elses.
 
Old 11-13-2012, 08:22 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
There's a missing if after
Code:
if [“$RESPONSE” = "a"]; then
#Create Users
echo "What is your NAME?"
read NAME
if [“$NAME” -!= "Tiara"]; then
echo "Your name is $NAME"
useradd -c $NAME
else
echo "That's my name too."
fi
46 of the double quotes are sloping double quotes instead of straight.

As already pointed out, -!= should be !=

In the Create Links and File Permissions sections the variables are not initialised before use.

As already pointed out, a case statement would be much better than all those ifs but maybe this exercise comes in the course before the case statement has been introduced and will be modified to use case later -- cleverly showing the student how much neater a case based solution is.

The script should exit after the "please try again" errors or the user does ot have an opportunity to try again.

Error messages are conventionally written to stderr, not echo's default stdout. Maybe this convention has not been introduced yet.

A function to test directory existence and create a directory would be much neater. Maybe functions have not been introduced yet.

EDIT:

The Create Links section does not have a ln command (the first ls should be ln -s ?)

Debugging would be easier if the commands were only displayed, not executed. This could be done by using a $debug variable as shown below (change to debug= when debugged).
Code:
#!/bin/bash
#Individual Project 5 for UNIX CS126
#Create a Menu
echo "Choose an option
a. Create Users
b. Create Groups
c. Create Directories
d. Create Links
e. Set File Permissions
f. exit"
read RESPONSE
echo "DEBUG: RESPONSE is '$RESPONSE'"
debug=echo
if [ "$RESPONSE" = "a" ]; then
    #Create Users
    echo "What is your NAME?"
    read NAME
    if [ "$NAME" != "Tiara" ]; then
        echo "Your name is $NAME"
        $debug useradd -c $NAME
    else
        echo "That's my name too."
    fi
fi
#Create Groups
if [ "$RESPONSE" = "b" ]; then
    echo "What is the name of the GROUP?"
    read GROUP
    if test -g $GROUP; then
        echo "That group name exists, please try again."
    else
        echo "The name of the group is $GROUP."
        $debug groupadd $GROUP
    fi
fi
#Create Directories
if [ "$RESPONSE" = "c" ]; then
    echo "What is the name of the DIRECTORY1?"
    read DIRECTORY1
    if test -d $DIRECTORY1; then
        echo "That directory name exists, please try again."
    else
        echo "The name of the directory is $DIRECTORY1."
        $debug mkdir $DIRECTORY1
        ls -l
        echo "What is the name of the DIRECTORY2?"
        read DIRECTORY2
        if test -d $DIRECTORY2; then
            echo "That directory name exists, please try again."
        else
            echo "The name of the directory is $DIRECTORY2."
            $debug mkdir $DIRECTORY2
            ls -l
            echo "What is the name of the DIRECTORY3?"
            read DIRECTORY3
            if test -d $DIRECTORY3; then
                echo "That directory name exists, please try again."
            else
                echo "The name of the directory is $DIRECTORY3."
                $debug mkdir $DIRECTORY3
                ls -l
                echo "What is the name of the DIRECTORY4?"
                read DIRECTORY4
                if test -d $DIRECTORY4; then
                    echo "That directory name exists, please try again."
                else
                    echo "The name of the directory is $DIRECTORY4."
                    $debug mkdir $DIRECTORY4
                    ls -l
                fi
            fi
        fi
    fi
fi
#Create Links
if [ "$RESPONSE" = "d" ]; then
    echo "Links all directories together."
    cd $DIRECTORY1
    ls $DIRECTORY2_ln $DIRECTORY3_ln $DIRECTORY4_ln
    ls
fi
#File Permissions
if [ "$RESPONSE" = "e" ]; then
    echo " This will modify permissions."
    $debug usermod -g $GROUP $NAME
    groups $NAME
    $debug chown $NAME $DIRECTORY1
    ls -l
    $debug chmod u+x, g+w $DIRECTORY1
    $debug chmod u+x, g-r, o-r $DIRECTORY2
    $debug chmod u+x, g-r, o-r $DIRECTORY3
    $debug chmod u+x, g-r, o-r $DIRECTORY4
    ls -l
fi

Last edited by catkin; 11-13-2012 at 08:26 PM.
 
Old 11-13-2012, 08:58 PM   #4
itsjustme
Senior Member
 
Registered: Mar 2003
Location: Earth
Distribution: Slackware, Ubuntu, Smoothwall
Posts: 1,571

Original Poster
Rep: Reputation: 45
I told her I thought the -!= was wrong. :-D I had never seen that, but like I said, I'm not familiar with bash script syntax.

After I posted this I went downstairs and had dinner and she called me and said she had changed it to use a case statement and said it was working! She's pretty smart.

She is going to send me the latest code. I'll post it up when I see it.

I'll also point her back over to this thread and look at your suggestions, etc.

Thanks for your input!

Last edited by itsjustme; 11-13-2012 at 09:04 PM.
 
Old 11-14-2012, 08:49 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,239

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
Agree with all above comments, its mostly small details; nonetheless critical for proper functioning...

You/she may find these worth bookmarking if you/she haven't already
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
  


Reply


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM


All times are GMT -5. The time now is 08:26 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration