Find the answer to your Linux question:
Results 1 to 4 of 4
Hi all, Im a complete novice to linux scripting, and my code is not working. Please help me, i am getting errors on lines 25, 34 and 38 when the ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    5

    [SOLVED] Help for the Noob: menu script

    Hi all,
    Im a complete novice to linux scripting, and my code is not working. Please help me, i am getting errors on lines 25, 34 and 38 when the respective options are chosen from the menu. Here is the code i have;
    Code:
    while :
    do
    clear
    echo "Main - Menu"
    echo "What would you like to do?..."
    echo ""
    echo "1. Show date and time"
    echo "2. Users Logged on"
    echo "3. Create a directory"
    echo "4. Start text editor (joe)"
    echo "5. Exit"
    echo ""
    echo -n "Please enter your choice (1 - 5):"
    read opt
    case $opt in
    1) echo "";
       echo "Todays date is `date '+%d/%m/%y'`";
       echo "The current time is `date '+%H:%M'`";
       echo "";
       echo "Press [enter] to return to the menu";
    read enterkey;;
    2) echo "";
       echo "Please enter the username to check";
    read username;
    while [$(who | grep $username)!=0]
    do
       echo "That user is not currently logged in";
    done;
    echo "";
       echo "Press [enter] to return to the menu";
    read enterkey;;
    3) 	echo "Please enter a name for your directory:"
      	read DIRNAME
    	if [!-d "$DIRNAME" -a !-f "%DIRNAME"]
    	then
    	echo "Creating directory $DIRNAME"
    	mkdir ~/$DIRNAME;
    	elif [-d "$DIRNAME"]
    	then 
    	echo Directory $DIRNAME already exists
    	else
    	echo File %DIRNAME already exists
    	fi;
    	echo "Press [enter] to return to the menu";
    	read enterkey;;
    4) joe newfile;; 
    5) echo "Goodbye $USER";
    exit 1;;
    *) echo "$opt is an invalid option. Please select an option between 1 and 5";
    echo "Press [enter] to continue...";
    read enterkey;;
    esac
    done
    If any of you can help id be most appreciative, thanks
    Last edited by daark.child; 03-22-2010 at 02:21 PM. Reason: Put code tags

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    this line

    while [$(who | grep $username)!=0]

    you should have spaces after [ and before ]...like below

    while [ $(who | grep $username)!=0 ]
    Make mine Arch Linux

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Actually you have some problems with your script. I took a small section, choice 2, and modified it. Is this what you want?

    Code:
    #! /bin/sh
    
    
    echo -n "Please enter the username to check->"
    read username
    
    if  who | grep $username
    then
    	:
    else
    	echo "'$username' is not currently logged in";
    fi
    
    exit 0
    Note: If your going to use line numbers to identify your errors please include them in your posting...
    Make mine Arch Linux

  4. #4
    Just Joined!
    Join Date
    Mar 2010
    Posts
    5
    Thats great gerard, i cant thank you enough - exactly what i was trying to acheive

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...