Need help creating bash script that creates directories based on what you answer
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
#!/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"
#!/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.
#!/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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.