Find the answer to your Linux question:
Results 1 to 4 of 4
Hello all, New here, and looking for assistance. I am in need of a little help with my Linux project. We have until Friday to finish this, but since we ...
  1. #1
    Just Joined! Atsumi's Avatar
    Join Date
    Oct 2008
    Posts
    2

    Newbie Case Question

    Hello all,

    New here, and looking for assistance.

    I am in need of a little help with my Linux project. We have until Friday to finish this, but since we got the assignment today, I want to turn it in tomorrow.

    Code:
    #!/bin/bash
    
    clear
    while :
    do
    echo "1. File Operations"
    echo "2. User Operations"
    echo "3. Locating Information"
    echo "4. Fun Stuff"
    echo "5. Exit"
    echo "6. Shut Down"
    read selection
    case $selection in
    
    	1 ) clear
    	while [ $fileselection != 9 ]
    	do
    	echo "FILE OPERATIONS"
    	echo "1. Create a File"
    	echo "2. Delete a File"
    	echo "3. Create a Directory"
    	echo "4. Delete a Directory"
    	echo "5. Create a Symbolic Link"
    	echo "6. Change Ownership of a File"
    	echo "7. Change Permissions on a File"
    	echo "8. Modify Text Within a File"
    	echo "9. Return to Main Menu"
    	echo "10. Shut Down"
    	read fileselection
    	case $fileselection in
    
    		1 ) clear
    		echo "CREATE A FILE"
    		echo "What would you like the file to be named?"
    		read filename
    		touch $filename
    		clear
    		;;
    
    		2 ) clear
    		echo "DELETE A FILE"
    		echo "Which file would you like to delete?"
    		read filedel
    		echo "Are you sure you want to delete $filedel? (Yes or No)"
    		rm -r $filedel
    		;;
    
    		3 ) clear
    		echo "CREATE A DIRECTORY"
    		echo "What would you like the directory to be named?"
    		read dirname
    		mkdir $dirname
    		;;
    	
    		4 ) clear
    		echo "DELETE A DIRECTORY"
    		echo "Which directory would you like to delete?"
    		read dirdel
    		rm -r $dirdel
    		;;
    
    		5 ) clear
    		echo "CREATE A SYMBOLIC LINK"
    		echo "Which file would you like to link? (FULL PATH)"
    		read filelink
    		echo "Where would you like to link it?(FULL PATH)"
    		read srclink
    		ln -s $srclink $filelink
    		;;
    
    		6 ) clear
    		echo "CHANGE OWNERSHIP OF A FILE"
    		echo "Which file would you like to change the ownership of?"
    		read fileown
    		echo "Who would you like the owner to be?"
    		read ownerown
    		echo "What group?"
    		read groupown
    		chown $owneroen.$groupown $fileown
    		;;
    
    		7 ) clear
    		echo "CHANGE PERMISSIONS ON A FILE"
    		echo "Which file would you like to change the permissions on?"
    		read fileperm
    		echo "What would you like those permissions to be? (3 DIGIT NUMERICAL)"
    		read filenum
    		chmod $filenum $fileperm
    		;;
    
    		8 ) clear
    		echo "MODIFY TEXT WITHIN A FILE"
    		echo "Which file would you like to change the text in?"
    		read filetxt
    		vi $filetxt
    		;;
    
    		9 ) clear
    		./master
    		;;
    
    		10 ) clear
    		shutdown -h now
    		;;
    		esac
    		done	
    ;;
    
    	2 ) clear
    	while :
    	do
    	echo "USER OPERATIONS"
    	echo "1. Create a User"
    	echo "2. Change Group of User"
    	echo "3. Create a Group"
    	echo "4. Delete a User"
    	echo "5. Change a Password"
    	echo "6. Return to Main Menu"
    	echo "7. Shut Down"
    	read userselection
    	case $userselection in
    	
    		1 ) clear
    		echo "CREATE A USER"
    		echo "What would you like the user to be named?"
    		read username
    		useradd $username
    		;;
    	
    		2 ) clear
    		echo "CHANGE GROUP OF USER"
    		echo "Which user would you like to change?"
    		read userchng
    		echo "Which group would you like to add them to?"
    		read groupchng
    		usermod -g $groupchng $userchang
    		;;
    
    		3 ) clear
    		echo "CREATE A GROUP"
    		echo "What would you like the group to be called?"
    		read $groupadd
    		groupadd $groupadd
    		;;
    
    		4 ) clear
    		echo "DELETE A USER"
    		echo "Which user would you like to delete?"
    		read userdel
    		userdel $userdel
    		;;
    
    		5 ) clear
    		echo "CHANGE A PASSWORD"
    		echo "Which user would you like to change the password of?"
    		read passchng
    		passwd $passchng
    		;;
    		
    		6 ) clear
    		./master
    		;;
    
    		7 ) clear
    		shutdown -h now
    		;;
    		esac
    		done
    
    	3 ) clear
    	while :
    	do
    	echo "LOCATING INFORMATION"
    	echo "1. Find Text Within a File"
    	echo "2. Show Information about a User Account"
    	echo "3. List the Contents of a Directory"
    	echo "4. Return to Main Menu"
    	echo "5. Shut Down"
    	read locate
    	case $locate in
    	
    		1 ) clear
    		echo "Find Text Within a File"
    		echo "Which file?"
                   #Working on this
    		;;
    
    
    		2 ) clear
    		echo "SHOW INFORMATION ABOUT A USER ACCOUNT"
    		/etc/passwd
    		;;
    
    		3 ) clear
    		echo "LIST THE DOCUMENTS IN A DIRECTORY"
    		ls -la
    		;;
    
    		4 ) clear
    		./master
    
    		5 ) shutdown -h now
    		;;
    		esac
    		done
    
    	4 ) clear
    	while :
    	do
    	echo "FUN STUFF"
    	#not sure what to put here yet.
    	;;
    	
    
    	5 ) clear
    	;;
    
    	6 ) shutdown -h now
    	;;
    
    
    esac
    read
    done
    My teacher has decided to make us figure out case in case by our selves, or ask online since "That's what Linuxheads do." So I'm looking for someone to help me perfect this code, and maybe come up with some "fun stuff" for that category.

  2. #2
    Linux Newbie
    Join Date
    Jan 2008
    Location
    UK
    Posts
    211
    hi atsumi welcome to the forum.
    first I would suggest you complete the code listing by adding in options and variables for the other case structures, or comment them out.
    you need to make sure any scripts such as ./master exist and are found by the script.

    Once this is done running the script should then start to throw up errors and broken or not understood commands / options which you can then work through.

    Hope this helps

    wowbag1

  3. #3
    Just Joined! Atsumi's Avatar
    Join Date
    Oct 2008
    Posts
    2
    Quote Originally Posted by wowbag1 View Post
    hi atsumi welcome to the forum.
    first I would suggest you complete the code listing by adding in options and variables for the other case structures, or comment them out.
    you need to make sure any scripts such as ./master exist and are found by the script.

    Once this is done running the script should then start to throw up errors and broken or not understood commands / options which you can then work through.

    Hope this helps

    wowbag1
    Thanks. ./master is the name of the script itself, it's calling itself back up for the main menu.

    The error I'm getting most of the time when trying to run it is "Unexpected Ending", though it looks correct to me.

  4. #4
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    There are at least 3 ;;'s missing for the case statement (2 are before and after the top level option #3 and 1 after the 2nd level option #4)
    and 1 done missing for the while statement.

    Also I would suggest you to use select statement to display the menu. It'll make the code much cleaner. For example, your top level menu can be written as
    Code:
    #!/bin/bash
    PS3=""
    select action in  "File Operations.." "User Operations.." "Locating Information.." "Fun Stuff" "Exit" "Shut Down"
    do
      case $REPLY in
        1) echo "file operations.." ;;
        2) echo "user operations.." ;;
        3) echo "location information..";;
        4) echo "fun stuff" ;;
        5) echo "exit"; exit 0 ;;
        6) echo "shut down" ;;
      esac
    done
    

Posting Permissions

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