LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash script help (https://www.linuxquestions.org/questions/linux-general-1/bash-script-help-4175437010/)

itsjustme 11-13-2012 05:53 PM

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...

unSpawn 11-13-2012 06:59 PM

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.

catkin 11-13-2012 08:22 PM

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


itsjustme 11-13-2012 08:58 PM

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!

chrism01 11-14-2012 08:49 PM

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/


All times are GMT -5. The time now is 04:16 PM.