Script parameters in Bash
The arguments that you provide to a bashscript will appear in the variables $1
and $2
and $3
where the number refers to the argument. $0
is the command itself.
The arguments are seperated by spaces, so if you would provide the -from
and -to
in the command, they will end up in these variables too, so for this:
./ocrscript.sh -from /home/kristoffer/test.png -to /home/kristoffer/test.txt
You'll get:
$0 # ocrscript.sh
$1 # -from
$2 # /home/kristoffer/test.png
$3 # -to
$4 # /home/kristoffer/test.txt
It might be easier to omit the -from
and the -to
, like:
ocrscript.sh /home/kristoffer/test.png /home/kristoffer/test.txt
Then you'll have:
$1 # /home/kristoffer/test.png
$2 # /home/kristoffer/test.txt
The downside is that you'll have to supply it in the right order. There are libraries that can make it easier to parse named arguments on the command line, but usually for simple shell scripts you should just use the easy way, if it's no problem.
Then you can do:
/usr/local/bin/abbyyocr9 -rl Swedish -if "$1" -of "$2" 2>&1
The double quotes around the $1
and the $2
are not always necessary but are adviced, because some strings won't work if you don't put them between double quotes.
Bash script passing parameter with spaces using variables
You should change CMD="curl -H $HEADER $URL"
in CMD="curl -H \"$HEADER\" \"$URL\""
.
How can I write a bash script that takes parameters in any given order?
Here's the script I wrote to achieve this:
#!/bin/bash
# The code below was written to replace the -print and -delete options for
# -p and -d respectively, because getopts can't handle long args, I suggest
# you only use arguments of single letters to make your code more simple, but if
# you can't avoid it then this is a workaround
for ARGS in "$@"; do
shift
case "$ARGS" in
"-print") set -- "$@" "-p" ;;
"-delete") set -- "$@" "-d" ;;
*) set -- "$@" "$ARGS"
esac
done
# getopts works like this: you put all your arguments between single quotes
# if you put a ':' character after the argument letter (just like I did with
# the 'f' letter in the example below), it means this argument NEEDS an extra
# parameter. If you just use letters without the ':' it means it doesn't need
# anything but the argument itself (it's what I did for the '-p' and '-d' options)
while getopts 'f:pd' flag; do
case "${flag}" in
f) FILE=${OPTARG} ;;
p) COMMAND="print" ;;
d) COMMAND="delete" ;;
esac
done
echo "$COMMAND $FILE"
And below examples of it running:
$ ./script.sh -f filename -print
print filename
$ ./script.sh -print -f filename
print filename
$ ./script.sh -f filename -delete
delete filename
$ ./script.sh -delete -f filename
delete filename
Bash script with both parameter values and optional flags
You should replace getopts u:a:f:h:
with getopts u:a:f:h
.
Removing the :
you tell getopts
that h
has no additional argument.
How to pass Named Parameter in Shell Script
You may want to read the getopts manual page
while getopts "U:P:J:I:" flag
do
case "${flag}" in
U) TEST_USER=${OPTARG};;
P) TEST_PWD=${OPTARG};;
J) TEST_JOBID=${OPTARG};;
I) TEST_PROJECTID=${OPTARG};;
esac
done
echo "USER: $TEST_USER";
echo "PWD: $TEST_PWD";
echo "JOBID: $TEST_JOBID";
echo "PROJECTID: $TEST_PROJECTID";
./getopts.sh -U devops@gmail.com -P xxxxxx -J 8a809e2496 -I 80e2ea54b231f
USER: devops@gmail.com
PWD: xxxxxx
JOBID: 8a809e2496
PROJECTID: 80e2ea54b231f
With getopt, it's more complicated, but it allows long options and "=".
#!/bin/bash
TEMP=$(getopt -n "$0" -a -l "user:,password:,jobid:,projectid:" -- -- "$@")
[ $? -eq 0 ] || exit
eval set -- "$TEMP"
while [ $# -gt 0 ]
do
case "$1" in
--user) TEST_USER="$2"; shift;;
--password) TEST_PWD="$2"; shift;;
--jobid) TEST_JOBID="$2"; shift;;
--projectid) TEST_PROJECTID="$2"; shift;;
--) shift;;
esac
shift;
done
echo "USER: $TEST_USER";
echo "PWD: $TEST_PWD";
echo "JOBID: $TEST_JOBID";
echo "PROJECTID: $TEST_PROJECTID";
Some tests:
$ ./test.sh -user=jules -password=kabas -jobid 5555 -projectid 999 -c
./test.sh: unrecognized option '-c'
$ ./test.sh -user=jules -password=kabas -jobid 5555 -projectid 999
USER: jules
PWD: kabas
JOBID: 5555
PROJECTID: 999
Shell how to make some of the script arguments mandatory
I made this work by checking that the variables req
and out
have some value after the for loop with the below code:
if [[ -z $out || -z $req ]]
then
echo "ERROR: -q and -o are mandatory arguments. See usage: \n";
exit 1;
fi
wget bash script with parameters
The standard format for the bash
(or sh
or similar) command is bash scriptfilename arg1 arg2 ...
. If you leave off all the first argument (the name or path of the script to run), it reads the script from stdin. Unfortunately, there's no way to leave off the firs argument but pass the others. Fortunately, you can pass /dev/stdin
as the first argument and get the same effect (at least on most unix systems):
wget -O - https://myserver/install/Setup.sh | bash /dev/stdin parameter1
If you're on a system that doesn't have /dev/stdin, you might have to look around for an alternative way to specify stdin explicitly (/dev/fd/0 or something like that).
Edit: Léa Gris suggestion of bash -s arg1 arg2 ...
is probably a better way to do this.
How to pass two parameters or arguments in bash scripting
As a start you can do this:
#!/bin/bash
dayoffset=$1
processMode=$2
echo "Do something with $dayoffset and $processMode."
Usage:
./start.sh 1 true
Another:
#!/bin/bash
while [[ $# -gt 0 ]]; do
case "$1" in
-dayoffset)
day_offset=$2
shift
;;
-processMode)
if [[ $2 != true && $2 != false ]]; then
echo "Option argument to '-processMode' can only be 'true' or 'false'."
exit 1
fi
process_mode=$2
shift
;;
*)
echo "Invalid argument: $1"
exit 1
esac
shift
done
echo "Do something with $day_offset and $process_mode."
Usage:
./start.sh -dayoffset 1 -processMode true
Example argument parsing with day offset:
#!/bin/bash
dayoffset=$1
date -d "now + $dayoffset days"
Test:
$ bash script.sh 0
Fri Aug 15 09:44:42 UTC 2014
$ bash script.sh 5
Wed Aug 20 09:44:43 UTC 2014
Related Topics
How to Automatically Redirect Http to Https on Apache Servers
Better Way to Rename Files Based on Multiple Patterns
Asynchronous Io Io_Submit Latency in Ubuntu Linux
Setting the Umask of the Apache User
Modify Qt Gui from Background Worker Thread
How Does Mmap Improve File Reading Speed
Execute Command After Every Command in Bash
Fast Concatenation of Multiple Gzip Files
Encryption/Decryption Doesn't Work Well Between Two Different Openssl Versions
Syntax Error in Shell Script With Process Substitution
How to Find Out Line-Endings in a Text File
How to Loop Over the Output of a Shell Command
How to Access the System Call from User-Space
Bash Command Not Found When Setting a Variable
Assembly Segmentation Fault After Making a System Call, At the End of My Code