LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 03-20-2013, 09:36 PM   #1
Profound_Darkness
LQ Newbie
 
Registered: Mar 2013
Posts: 2

Rep: Reputation: Disabled
Need help creating bash script that creates directories based on what you answer


I need a script that asks the user where they work and based on their answer it creates a directory on the users desktop with a correct folder name. I'm really new to scripting so I know the script completly wrong

Example: This is just a real quick example of what I need

Code:
#!/bin/bash

echo "Where do you work? 1:front 2:middle 3:back
read number

if number = 1
 then
 mkdir /$home/Desktop/front
 echo "your folder has been created"
 exit

if number = 2
 then
 mkdir /$home/Desktop/middle
 echo "your folder has been created"
 exit

if number = 3
 then
 mkdir /$home/Desktop/back
 echo "your folder has been created"
 exit
Thanks for any help

Last edited by onebuck; 03-22-2013 at 09:58 AM. Reason: add vbcode tags to clean post, Please learn to use code tags
 
Old 03-20-2013, 10:40 PM   #2
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,748

Rep: Reputation: 1283Reputation: 1283Reputation: 1283Reputation: 1283Reputation: 1283Reputation: 1283Reputation: 1283Reputation: 1283Reputation: 1283
Hi,

the variable "$home" is probably not defined. Perhaps you mean "$HOME"? Also you should not need the leading slash. Eg

Code:
mkdir $HOME/Destop/front
Next problem seems to be the way you are using conditionals in if and you are missing the "fi" (that terminates the "if" block). Try
Code:
if [ "$number" = "1" ]
then
  mkdir $HOME/Destop/front
fi
HTH,

Evo2.
 
Old 03-21-2013, 04:11 AM   #3
kooru
Senior Member
 
Registered: Sep 2012
Location: Italy
Distribution: Slackware, NetBSD
Posts: 1,382
Blog Entries: 5

Rep: Reputation: 272Reputation: 272Reputation: 272
There are some syntax errors
Try this:

Code:
#!/bin/bash

echo "Where do you work? 1:front 2:middle 3:back"
read number

  if [ $number -eq 1 ]; then
     mkdir $HOME/Desktop/front
     echo "your folder has been created"
  fi

  if [ $number -eq 2 ]; then
     mkdir $HOME/Desktop/middle
     echo "your folder has been created"
  fi

  if [ $number -eq 3 ]; then
     mkdir $HOME/Desktop/back
     echo "your folder has been created"
  fi

echo "Thanks LQ :D"
 
Old 03-21-2013, 05:52 PM   #4
Profound_Darkness
LQ Newbie
 
Registered: Mar 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks for the quick response and the help. I'll give this a shot and if I have any more question i'll make sure to come back here.
 
Old 03-23-2013, 05:24 PM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,612

Rep: Reputation: 486Reputation: 486Reputation: 486Reputation: 486Reputation: 486
Quote:
Originally Posted by kooru View Post
There are some syntax errors
Try this:

Code:
#!/bin/bash

echo "Where do you work? 1:front 2:middle 3:back"
read number

  if [ $number -eq 1 ]; then
     mkdir $HOME/Desktop/front
     echo "your folder has been created"
  fi

  if [ $number -eq 2 ]; then
     mkdir $HOME/Desktop/middle
     echo "your folder has been created"
  fi

  if [ $number -eq 3 ]; then
     mkdir $HOME/Desktop/back
     echo "your folder has been created"
  fi

echo "Thanks LQ :D"
you could even add the destination in the echo reply:

Code:
  if [ $number -eq 1 ]; then
     mkdir $HOME/Desktop/front
     echo "your folder has been created at {$HOME}/Desktop/front."
  fi
for those out there who are much better coders then I am, would this not be a great place to use elif to tie all three responses together?

Code:
#!/bin/bash

echo "Where do you work? 1:front 2:middle 3:back"
read number

     if [ ${number} -eq 1 ]
          then
          mkdir ${HOME}/Desktop/front
          echo "Your folder has been created at ${HOME}/Desktop/front."
     
     elif [ ${number} -eq 2 ]
          then
          mkdir ${HOME}/Desktop/middle
          echo "Your folder has been created at ${HOME}/Desktop/middle."

     elif [ ${number} -eq 3 ]
           then
           ${HOME}/Desktop/back
           echo "Your folder has been created at ${HOME}/Desktop/back."
fi

echo "Thanks LQ :D"
exit
I am not 100% you can do that, please those of you with much better bash scripting please tell me if im write or wrong and help me if im wrong. Thanks.
 
Old 03-23-2013, 06:24 PM   #6
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,825

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
If I may add a comment on the use of brackets:
Quote:
if [ $number -eq 1 ]; then
The single square bracket is a bash builtin command. As such, the command must be separated by at least one space from the thing the command works on.
Quote:
if [$number -eq1 ]; then
Remove the space between the bracket and the test, and you will get an error instead of what you expect.

Just something to keep in mind when debugging a script which uses either single or double square brackets.

PS: you might also use a case argument instead of if-elif-fi for choosing which action to take.

Last edited by bigrigdriver; 03-23-2013 at 06:25 PM.
 
Old 03-23-2013, 08:33 PM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,612

Rep: Reputation: 486Reputation: 486Reputation: 486Reputation: 486Reputation: 486
Quote:
Originally Posted by bigrigdriver View Post
If I may add a comment on the use of brackets:

The single square bracket is a bash builtin command. As such, the command must be separated by at least one space from the thing the command works on.

Remove the space between the bracket and the test, and you will get an error instead of what you expect.

Just something to keep in mind when debugging a script which uses either single or double square brackets.

PS: you might also use a case argument instead of if-elif-fi for choosing which action to take.
im still rather new to scripting, could you give example of the case options for this?
 
Old 03-23-2013, 10:04 PM   #8
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,612

Rep: Reputation: 486Reputation: 486Reputation: 486Reputation: 486Reputation: 486
the case esac seems to be semi close to getops so far as its syntax. am i right on that?
 
Old 03-23-2013, 10:13 PM   #9
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,612

Rep: Reputation: 486Reputation: 486Reputation: 486Reputation: 486Reputation: 486
Code:
#!/bin/bash

### test to see if I understand case esac for the OP ###

echo "Where do you work? 1:front 2:middle 3:back"

case "$1" in

	1)	echo "You work in the front."
		mkdir ${HOME}/Desktop/front
		echo "Your folder has been created at ${HOME}/Desktop/front."
		;;

	2)	echo "You work in the middle."
		mkdir ${HOME}/Desktop/middle
		echo "Your folder has been created at ${HOME}/Desktop/middle."
		;;

	3)	echo "You work in the back."
		mkdir ${HOME}/Desktop/back
		echo "Your folder has been created at ${HOME}/Desktop/back."
		;;

	*)	echo "Invalid option"
		;;
esac

echo "Thanks LQ :D"

exit
do i have that right?
 
Old 03-24-2013, 10:16 AM   #10
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,825

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
That looks good.

The proof of whether or not it's correct is to try to run it. If it works, it's right. If it fails, debugging is needed to make it work.
 
Old 03-26-2013, 10:34 AM   #11
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,612

Rep: Reputation: 486Reputation: 486Reputation: 486Reputation: 486Reputation: 486
Quote:
Originally Posted by bigrigdriver View Post
That looks good.

The proof of whether or not it's correct is to try to run it. If it works, it's right. If it fails, debugging is needed to make it work.
thanks, as long as it looks good, this was more for the OP and a learning opportunity for me then anything else. i has no desire to run that on my server .

Was just looking for some feedback to ensure i got the syntax correct for the case esac.
 
  


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
Elegant bash script needed to make new directories based on matched patterns emmalg Linux - General 4 03-11-2013 05:24 AM
how to create a bash script that creates a bar graph in the terminal window Satsugyo Linux - Newbie 10 11-26-2012 01:37 AM
Bash Script - executing answer to another script domz Programming 4 11-07-2010 04:19 PM
bash/php script which creates linux user... tifoso Linux - General 1 11-05-2009 06:18 AM
Bash script creates blank file name snowman81 Programming 9 03-02-2008 10:37 PM


All times are GMT -5. The time now is 04:50 AM.

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