ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Hi,
I just started learning it last this past friday.
I'm not sure what's wrong. Even if I hit enter, it says I've input a string.
I want to set a default value if nothing is input.
Also, on occasion, the -z switch does not seem to work. I'm on Bash 3.x
Any tips are welcome too.
Code:
#!/bin/bash
set -e
DOWNLOAD_DIR="/var/www/downloads"
make_main_dl_dir () {
clear
echo -n "Enter the path to download files\nDefault is ${DOWNLOAD_DIR} \n> "
read DOWNLOAD_DIR_USER
if [ -z DOWNLOAD_DIR_USER ]; then
echo "No input. Default value used.\n"
else
echo "String entered.\n"
if [ -d ${DOWNLOAD_DIR_USER} ]; then
echo "${DOWNLOAD_DIR_USER} already exists but will be used anyway.\n"
fi
DOWNLOAD_DIR=${DOWNLOAD_DIR_USER}
fi
echo "\v\vDownload dir: ${DOWNLOAD_DIR}\n"
exit 0
}
make_main_dl_dir;
exit 0
This won't work. First, $DOWNLOAD_DIR_USER needs a dollar sign to indicate that it's a variable. Secondly, it it's an emty string, then this will be interpreted as:
Code:
if [ -z ]; then
which is a syntax error. So you need to enclose it in double-quotes.
Personally, I prefer to use if blocks to assign a default value to a variable, because this makes it easier to trap cases of non-empty but invalid input.
thanks for that explanation i will be trying that out tomorrow and i will probably have some questions regarding this.
ok i've modified my script to this:
Code:
make_main_dl_dir () {
clear
echo -n "Enter the path to download files\nDefault is ${DOWNLOAD_DIR} \n> "
read DOWNLOAD_DIR_USER
if [ -z ${DOWNLOAD_DIR_USER} ]; then
echo "No input. Default value used.\n"
else
echo "String entered.\n"
if [ -d ${DOWNLOAD_DIR_USER} ]; then
echo "${DOWNLOAD_DIR_USER} already exists but will be used anyway.\n"
fi
DOWNLOAD_DIR=${DOWNLOAD_DIR_USER}
fi
echo "\v\vDownload dir: ${DOWNLOAD_DIR}\n"
ALL_DL_DIRS="${DOWNLOAD_ATOMIC} ${DOWNLOAD_PGSQL}"
for each_dir in ${ALL_DL_DIRS} ;
do
mkdir ${each_dir}
if [ -d ${each_dir} ]; then
echo "Directory ${each_dir} was created"
else
echo "Directory ${each_dir} could not be created. Please make sure you are the root user."
fi
done
exit 0
}
does anything seem out of place here?
suppose the directory could not be created for some reason, will the script stop if i have 'set -e' at the beginning?
also, is there a place where i can look up best practiced in bash scripting? the reason i'm asking you instead of looking on google is that, i was looking up creating functions in bash and quite a few sites had something similar to this example:
Code:
function foo
{
echo 'something'
}
but this did not work for me. i'm guessing it's a version related issue?
however, after much trial and error, i found that this worked for me:
Code:
foo () {
echo 'something'
}
thanks for everything so far, it's been very useful
PS:
which is the 'right way' to assign default values?
That really depends on what you are doing. The := will use your default value if your variable is not set. The :- will use the default value if the value is empty or if it is not set. I can't say that I can figure out how a variable is set without a value, but the two seem to be pretty much the same.
It should be noted that you can also use ${var-value} and ${var=value} to set default values as well. I'd really suggest reading through the Advanced Bash-Scripting Guide that I mentioned above. I have found it to be a VERY good source of information, and has a whole section dealing with parameter substitution.
is actually a cheat, so that I can define all my default variables in the same place
if you have it alone on the line you get...
Code:
dysp0024:primalA$ ${VAR:=thang}
ksh: thing: not found
the colon makes it a label, so I can do this...
Code:
: ${VAR:=thing}
it will be treated as a command
and the shell will try and run it. The colon makes it a label so it gets set but is not evaled as a
command.
as for := and :- it's all in man ksh or bash
:= actually sets it if null :- just uses it if null without setting e.g:
Code:
dysp0024:$ echo ${THIS}
dysp0024$ echo ${THIS:-or that}
or that
dysp0024$ echo ${THIS}
Great, thanks everyone
I'm making progress with my scrip thanks to this forum.
I want to create a nested menu though, what's the best way to do this?
For example, I have this menu:
Quote:
MAIN MENU
1 - Manage HTTPD
2 - Manage Database
0 - Exit
Enter choice:
upon entering 1, for example, I would like to show:
Quote:
MAIN MENU
1 - Get HTTPD status
2 - Start HTTPD
3 - Stop HTTPD
4 - Restart HTTPD
5 - Reload HTTPD
6 - Graceful HTTPD
7 - Configtest HTTPD
8 - Fullstatus HTTPD
0 - Exit
Enter choice:
I can create simple menu's with CASE but what about nested menu's? Have the nested blocks in separate functions and then call them once an option is selected?
I would normally create a seperate function for sub menus.
Code:
f_sub_menu_a()
{
echo "Sub Menu"
## Rest of sub_menu_a goes here (include case statement)
}
## Further down the code (where you have your case statement for your menu:
1) f_sub_menu_a;;
The important thing is to have the function before the part of the script that calls it otherwise your script will error as the function hasnt been defined at that point.
Select is coved in section 10.4 of the Advanced Bash-Scripting Guide. If you want to change the prompt that is used for select, you will need to set the PS3 variable to what you would like it to be.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.