LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-05-2007, 05:58 AM   #1
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Rep: Reputation: 15
New to shell scripting. Need help with conditions


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
 
Old 11-05-2007, 06:02 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
[ -z "$DOWNLOAD_DIR_USER" ]
 
Old 11-05-2007, 07:44 AM   #3
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
Quote:
Originally Posted by koobi View Post
I want to set a default value if nothing is input.
You can set a default value for a variable as such ...

Code:
${DOWNLOAD_DIR_USER:-value}
So say you want to set that variable to Daniel (:- is for if the variable is not set, or if it is empty), you could do your read and then

Code:
DOWNLOAD_DIR_USER=${DOWNLOAD_DIR_USER:-Daniel}
Check out Section 9.3 of the Advanced Bash-Scripting Guide for parameter substitutions.
 
Old 11-05-2007, 07:53 AM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Quote:
Originally Posted by koobi View Post
Code:
    if [ -z DOWNLOAD_DIR_USER ]; then
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.

Hope that helps,


—Robert J Lee
 
Old 11-05-2007, 08:02 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
which is a syntax error
actually in this case it isn't,

though generally it's true for test statements.

you can do a default value like this:


: ${VARIABLE:=default}
 
Old 11-05-2007, 10:54 AM   #6
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by bigearsbilly View Post
[ -z "$DOWNLOAD_DIR_USER" ]
oops silly of me for missing that one. thanks



Quote:
Originally Posted by Hobbletoe View Post
You can set a default value for a variable as such ...

Code:
${DOWNLOAD_DIR_USER:-value}
So say you want to set that variable to Daniel (:- is for if the variable is not set, or if it is empty), you could do your read and then

Code:
DOWNLOAD_DIR_USER=${DOWNLOAD_DIR_USER:-Daniel}
Check out Section 9.3 of the Advanced Bash-Scripting Guide for parameter substitutions.
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?
Code:
${DOWNLOAD_DIR_USER:-value}
or

Code:
: ${VARIABLE:=default}
 
Old 11-05-2007, 11:04 AM   #7
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
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.
 
Old 11-06-2007, 02:14 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
the way I did it

: ${VAR:=thing}

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}
 
Old 11-08-2007, 11:35 PM   #9
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Original Poster
Rep: Reputation: 15
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?

Thanks.
 
Old 11-09-2007, 01:57 AM   #10
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
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.
 
Old 11-09-2007, 02:16 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you do menus with select

Code:
$ select choice in *; do 
     echo you picked $choice; 
     break; 
done

1) Proxy.tgz                        9) plan9.iso.bz2
2) Text-Indent-0.1.tar.gz          10) sol-nv-b70b-x86-dvd-iso-a.zip
3) ade.ftp                         11) sol-nv-b70b-x86-dvd-iso-b.zip
4) dtpdev.ftp                      12) sol-nv-b70b-x86-dvd-iso-c.zip
5) ftp2ade.bat                     13) sol_sums
6) ftp2dtpdev.bat                  14) suns_own_sums
7) ftp2vca.bat                     15) vca.ftp
8) md5sum_dvd_x86.list
#? 15
you picked vca.ftp
 
Old 11-09-2007, 02:24 AM   #12
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Billy is right select is the way do go! much cleaner.

Last edited by Disillusionist; 11-09-2007 at 02:32 AM.
 
Old 11-09-2007, 07:28 AM   #13
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
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.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell script - while loop with multiple conditions ronsha Programming 13 12-10-2005 04:08 PM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
shell scripting Prasun1 Linux - General 1 06-29-2005 06:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:42 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
Open Source Consulting | Domain Registration