Sponsored Content
Top Forums Shell Programming and Scripting Adding options to a shell script Post 302215004 by drl on Tuesday 15th of July 2008 09:58:17 AM
Old 07-15-2008
Hi.

Debugging shell scripts can be done with echo or set -x. The latter, when placed near the top of a script will produce lots of output, so here is a driver script that puts your echo-augmented script through its paces:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate getopts.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1)
set -o nounset
echo

U=./user1
echo " Contents of script $U:"
cat $U

echo
echo " For plain:"
$U

echo
echo " For illegal argument:"
$U -z

echo
echo " For missing option-arguments:"
$U -i
$U -l

echo
echo " For legitimate option arguments:"
$U -ix -l y -- file1 file2

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0

 Contents of script ./user1:
#!/bin/bash
        lflag=
        iflag=
        while getopts 'l:i:' OPTION
        do
          case $OPTION in
          l)    lflag=1
                        lval="$OPTARG"
                        ;;
          i)    iflag=1
                        ival="$OPTARG"
                        ;;
          ?)    printf "Usage: %s: [-a] [-b value] args\n" $(basename $0) >&2
                        exit 2
                        ;;
          esac
        done
shift $(($OPTIND - 1))

echo
echo " iflag = $iflag, ival = $ival; lflag = $lflag, lval = $lval"

echo
echo " Remaining command line:"
echo $*

 For plain:

 iflag = , ival = ; lflag = , lval =

 Remaining command line:


 For illegal argument:
./user1: illegal option -- z
Usage: user1: [-a] [-b value] args

 For missing option-arguments:
./user1: option requires an argument -- i
Usage: user1: [-a] [-b value] args
./user1: option requires an argument -- l
Usage: user1: [-a] [-b value] args

 For legitimate option arguments:

 iflag = 1, ival = x; lflag = 1, lval = y

 Remaining command line:
file1 file2

The driver script lists your script, then tests it. As you can see, I've added a few echo statements. The results look OK to me, so I'd guess you called it in an unconventional way.

As an aside: using l (ell), 1 (one), and i (eye) can be confusing to the user of a script because in many typefaces they all look very similar.

Best wishes ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding -options to shell scripts

I'm sure this is something simple I am overlooking somehow. I'd like the ability to pass -options into my shell scripts. For example my file called "input.sh" I can do the following: root# ./input.sh 1 and it will result: root#./input.sh 1 You passed me a 1 Just like an init script, etc.... (4 Replies)
Discussion started by: sysera
4 Replies

2. Shell Programming and Scripting

Shell script to invoke options automatically

i have a script which has 2 options. a b And a has 6 sub options. i want to write a script which will call the parent script and give options automatically. examle: linasplg11:/opt/ss/kk/01.00/bin # startup.sh /opt/ss/rdm/01.00 Please select the component to... (2 Replies)
Discussion started by: Aditya.Gurgaon
2 Replies

3. Shell Programming and Scripting

Help with shell script to run the commands reading options from local file

I have to use shell script to run series of commands on another unix box by connecting through SSH and giving user credentials. For running commands on remote machine I have to use options reading from a local file. Process: Connecting to remote unix server <host1.ibm.com> through ssh Login: ... (2 Replies)
Discussion started by: itsprout
2 Replies

4. Homework & Coursework Questions

Menu Driven Shell Script which accepts1 to 5 options

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1) Write a Menu Driven Shell Script which accepts1 to 5 options and performs the following actions for... (1 Reply)
Discussion started by: vaghya
1 Replies

5. Shell Programming and Scripting

shell script options

one thing i was trying to figure out is if you can give people the option to choose what they want to do in a shell script. for example, let's just say that you have a simple shell script to install a couple of programs, can you make it to where you can press a certain key to install a certain... (1 Reply)
Discussion started by: hotshot247
1 Replies

6. Shell Programming and Scripting

Write a shell script with options

Hi All I am little bit confused to write a script. This script needs the options like unix commands i.e. –S to start process. –C to check process. -u : user -p : password like. script should run like this ./script.sh -u username -p ***** -S processname there may... (5 Replies)
Discussion started by: atul9806
5 Replies

7. Shell Programming and Scripting

How to automate user selection options in shell script?

Hi There, I am trying to write a script which has to pick the prompted options by itself(i mean option to choose will be passed) here is real scenario i am trying to do. i have an executable(diagnos) which gets called in shell script, when the executable (diagnos) runs i get following as... (8 Replies)
Discussion started by: sairam_9191
8 Replies

8. Shell Programming and Scripting

Automate the menu options using shell script

I have a menu option which will look as follows Select a menu option 1.change password 2.login as root user 3.show system version 4.quit Select> 1 please enter the new password: unix reenter the new password: unix press any key to enter (then displays again the menu options to enter the... (4 Replies)
Discussion started by: shivakumar6g
4 Replies

9. Shell Programming and Scripting

How to include menu based options in Shell script?

Hi Friends, I have a menu based tool which requires input/option to proceed further. How to make a shell script ? eg: menu looks like Get_data.sh to continue (y/n) : Here I need to key in "y" to proceed. I want to prepare a script which should consider option y. (5 Replies)
Discussion started by: suresh3566
5 Replies

10. Shell Programming and Scripting

Adding Two Array in shell script

Hi Experts, I've got this problem where I need to add two array in shell script such that that is an element is greater than 9 then it get further split into individual digit, something like below : Input :- array1=(2 6 8 9 10 12) array2=(5 4 6 8 12 14) Output :- array3=(7 1 0 1 4 1 7 2 2... (8 Replies)
Discussion started by: mukulverma2408
8 Replies
CHECKBASHISMS(1)                                              General Commands Manual                                             CHECKBASHISMS(1)

NAME
checkbashisms - check for bashisms in /bin/sh scripts SYNOPSIS
checkbashisms script ... checkbashisms --help|--version DESCRIPTION
checkbashisms, based on one of the checks from the lintian system, performs basic checks on /bin/sh shell scripts for the possible presence of bashisms. It takes the names of the shell scripts on the command line, and outputs warnings if possible bashisms are detected. Note that the definition of a bashism in this context roughly equates to "a shell feature that is not required to be supported by POSIX"; this means that some issues flagged may be permitted under optional sections of POSIX, such as XSI or User Portability. In cases where POSIX and Debian Policy disagree, checkbashisms by default allows extensions permitted by Policy but may also provide options for stricter checking. OPTIONS
--help, -h Show a summary of options. --newline, -n Check for "echo -n" usage (non POSIX but required by Debian Policy 10.4.) --posix, -p Check for issues which are non POSIX but required to be supported by Debian Policy 10.4 (implies -n). --force, -f Force each script to be checked, even if it would normally not be (for instance, it has a bash or non POSIX shell shebang or appears to be a shell wrapper). --extra, -x Highlight lines which, whilst they do not contain bashisms, may be useful in determining whether a particular issue is a false posi- tive which may be ignored. For example, the use of "$BASH_ENV" may be preceded by checking whether "$BASH" is set. --version, -v Show version and copyright information. EXIT VALUES
The exit value will be 0 if no possible bashisms or other problems were detected. Otherwise it will be the sum of the following error val- ues: 1 A possible bashism was detected. 2 A file was skipped for some reason, for example, because it was unreadable or not found. The warning message will give details. SEE ALSO
lintian(1). AUTHOR
checkbashisms was originally written as a shell script by Yann Dirson <dirson@debian.org> and rewritten in Perl with many more features by Julian Gilbey <jdg@debian.org>. DEBIAN Debian Utilities CHECKBASHISMS(1)
All times are GMT -4. The time now is 09:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy