script - passing on command line options in bash
2014-01
I have a question. I have a script, a kind of long script written in bash aprox. 370 lines. That has several functions and in those functions the user has to enter information which is then stored in files. ( This is suppose to represent a MySQL database, with the functions INSERT, UPDATE, SELECT, SELECT where x=y.) I created this myself in bash, now the only thing that rests me is that I need to be able to pass arguments on the command line to the script, that will do the same as the script does. I know that bash has positional parameters such as
$1
$2
$3
$*
$@
$0 ( refers to the name of the script)
etc. I know how I can use these parameters in a simple if function. This is not enough for my script. I basicly need to do the same thing that the script does, but then from the command line. I have been struggling with this for a couple of days now and I cannot think of a way to get it to work. Maybe someone here can help me with this?
If you want to have the script. That can be possible, but I don't think I can paste it in here...
EDIT: Link to script, http://pastebin.com/Hd5VsDv2
Note, I am a beginner in bash scripting.
EDIT: This is in reply to Answer 1. As I said I hope I can just replace the if [ "$1" = "one" ] ; then echo "found one"
to if [ "$1" = "one" ] ; then echo SELECT
where SELECT is the function I previously had in my script(above)
http://pastebin.com/VFMkBL6g LINK to testing script
The script is interactive and reads it's data with several read
statements.
After a quick look my judgement is that it would be easier to write a new script than adjust the current script to accept command lines. (With liberal use of cut/paste it shouldn't be that hard).
A few tips to get you started.
- Write a simple script that reads three or four variables from the command line, saves the variables in local variables
A B C D
andecho
them out again. - In your script create a function that
if A=one echos "found one" if A=two echos "found two" if A=three echos "found two"
(case works well for this) - Create another few function that does the same thing as no2 above but for
B
,C
&D
- Replace the echo statments in no2 with a call to your newly created functions
- If you get this to work you can now start replacing your place-holder functions and values with real stuff from your current script.
Lastly; don't take to many steps at once and when you get stuck ask a question over on http://stackoverflow.com/. Be sure to include the minimum amount of code that can be run and reproduces the error.
Good luck
In bash there is built-in getopts
to parse positional parameters. You can also use older external command getopt
. Look at Using getopts to read the options/arguments passed to a script
I have a script that I want to accept any number of command line arguments.
So far I have
if [ -O $1 ] ; then echo "you are the owner of $1" else echo "you are not the owner of $1" fi
Obviously if I wanted the script to only accept one argument this would work but what would work for ANY number of arguments.
ex. ./script f f1 f2 f3
One possible way to do what you want involves $@
, which is an array of all the arguments passed in.
for item in "$@"; do
if [ -O "$item" ]; then
echo "you are the owner of $item"
else
echo "you are not the owner of $item"
fi
done
Well, it depends on exactly what you want to do, but look into $*
, $@
, and shift
.
"$@" does not solve his problem of "ANY number of arguments". there is a limit in how long a commandline can be (http://www.in-ulm.de/~mascheck/various/argmax/). a better way to read in "unlimited arguments" is via STDIN:
prg_which_creates_arguments | while read a; do \
echo "do something with $a"; \
done
just create the arguments and pipe them one after another at the code which is doing something with them.