I would like to update a script that is currently like this:
$ example.sh a b
Here is the code within example.sh
for var in "$#"
do
$var
done
Where it takes in arguments and those arguments are looped over and executed (assuming those arguments exist).
I would like to update the script so that these flags are the scripts / functions and that everything after is applied as the argument to the function.
$ example.sh --a 1 2 3 --b 4 5 6
I would like to loop over all flags and run the equivalent of.
a 1 2 3
b 1 2 3
I have looked into getopts but I am not sure if it will allow me to execute and pass in the arguments the way I would like.
What I tried:
while getopts ":a:b:c:d:" opt; do
case "$opt" in
a) i=$OPTARG ;;
b) j=$OPTARG ;;
c) k=$OPTARG ;;
d) l=$OPTARG ;;
esac
done
echo $i
echo $j
for file in "$#"; do
echo $file
done
I found the following script which given example --a 1 2 3 --b 4 5 6 will only assign the first item using OPTARG and it doesn't work properly. I am unsure how to apply arguments to a function in this format.
I don't know of any automatic way to do what you want, but you can just loop through your arguments and construct your commands, like this:
#!/bin/bash
cmd=()
while [ $# -gt 0 ]; do # loop until no args left
if [[ $1 = --* ]]; then # arg starts with --
[[ ${#cmd[#]} -gt 0 ]] && "${cmd[#]}" # execute previous command
cmd=( "${1#--}" ) # start new array
else
cmd+=( "$1" ) # append to command
fi
shift # remove $1, $2 goes to $1, etc.
done
[[ ${#cmd[#]} -gt 0 ]] && "${cmd[#]}" # run last command
Perhaps this way.
cat example.sh
while read line;do
$line
done <<<$(echo $# | sed 's/--/\n/g')
and I try that
./example.sh '--echo 1 2 3 --dc -e 4sili5+p'
output
1 2 3
9
Related
I am working on a Bash script that needs to take zero to multiple strings as an input but I am unsure how to do this because of the lack of a flag before the list.
The script usage:
script [ list ] [ -t <secs> ] [ -n <count> ]
The list takes zero, one, or multiple strings as input. When a space is encountered, that acts as the break between the strings in a case of two or more. These strings will eventually be input for a grep command, so my idea is to save them in an array of some kind. I currently have the -t and -n working correctly. I have tried looking up examples but have been unable to find anything that is similar to what I want to do. My other concern is how to ignore string input after a flag is set so no other strings are accepted.
My current script:
while getopts :t:n: arg; do
case ${arg} in
t)
seconds=${OPTARG}
if ! [[ $seconds =~ ^[1-9][0-9]*$ ]] ; then
exit
fi
;;
n)
count=${OPTARG}
if ! [[ $count =~ ^[1-9][0-9]*$ ]] ; then
exit
fi
;;
:)
echo "$0: Must supply an argument to -$OPTARG" >&2
exit
;;
?)
echo "Invalid option: -${OPTARG}"
exit
;;
esac
done
Edit: This is for a homework assignment and am unsure if the order of arguments can change
Edit 2: Options can be in any order
Would you please try the following:
#!/bin/bash
# parse the arguments before getopts
for i in "$#"; do
if [[ $i = "-"* ]]; then
break
else # append the arguments to "list" as long as it does not start with "-"
list+=("$1")
shift
fi
done
while getopts :t:n: arg; do
: your "case" code here
done
# see if the variables are properly assigned
echo "seconds=$seconds" "count=$count"
echo "list=${list[#]}"
Try:
#! /bin/bash -p
# Set defaults
count=10
seconds=20
args=( "$#" )
end_idx=$(($#-1))
# Check for '-n' option at the end
if [[ end_idx -gt 0 && ${args[end_idx-1]} == -n ]]; then
count=${args[end_idx]}
end_idx=$((end_idx-2))
fi
# Check for '-t' option at the (possibly new) end
if [[ end_idx -gt 0 && ${args[end_idx-1]} == -t ]]; then
seconds=${args[end_idx]}
end_idx=$((end_idx-2))
fi
# Take remaining arguments up to the (possibly new) end as the list of strings
strings=( "${args[#]:0:end_idx+1}" )
declare -p strings seconds count
The basic idea is to process the arguments right-to-left instead of left-to-right.
The code assumes that the only acceptable order of arguments is the one given in the question. In particular, it assumes that the -t and -n options must be at the end if they are present, and they must be in that order if both are present.
It makes no attempt to handle option arguments combined with options (e.g. -t5 instead of -t 5). That could be done fairly easily if required.
It's OK for strings in the list to begin with -.
My shorter version
Some remarks:
Instead of loop over all argument**, then break if argument begin by -, I simply use a while loop.
From How do I test if a variable is a number in Bash?, added efficient is_int test function
As any output (echo) done in while getopts ... loop would be an error, redirection do STDERR (>&2) could be addressed to the whole loop instead of repeated on each echo line.
** Note doing a loop over all argument could be written for varname ;do. as $# stand for default arguments, in "$#" are implicit in for loop.
#!/bin/bash
is_int() { case ${1#[-+]} in
'' | *[!0-9]* ) echo "Argument '$1' is not a number"; exit 3;;
esac ;}
while [[ ${1%%-*} ]];do
args+=("$1")
shift
done
while getopts :t:n: arg; do
case ${arg} in
t ) is_int "${OPTARG}" ; seconds=${OPTARG} ;;
n ) is_int "${OPTARG}" ; count=${OPTARG} ;;
: ) echo "$0: Must supply an argument to -$OPTARG" ; exit 2;;
? ) echo "Invalid option: -${OPTARG}" ; exit 1;;
esac
done >&2
declare -p seconds count args
Standard practice is to place option arguments before any non-option arguments or variable arguments.
getopts natively recognizes -- as the end of option switches delimiter.
If you need to pass arguments that starts with a dash -, you use the -- delimiter, so getopts stops trying to intercept option arguments.
Here is an implementation:
#!/usr/bin/env bash
# SYNOPSIS
# script [-t<secs>] [-n<count>] [string]...
# Counter of option arguments
declare -i opt_arg_count=0
while getopts :t:n: arg; do
case ${arg} in
t)
seconds=${OPTARG}
if ! [[ $seconds =~ ^[1-9][0-9]*$ ]] ; then
exit
fi
opt_arg_count+=1
;;
n)
count=${OPTARG}
if ! [[ $count =~ ^[1-9][0-9]*$ ]] ; then
exit 1
fi
opt_arg_count+=1
;;
?)
printf 'Invalid option: -%s\n' "${OPTARG}" >&2
exit 1
;;
esac
done
shift "$opt_arg_count" # Skip all option arguments
[[ "$1" == -- ]] && shift # Skip option argument delimiter if any
# Variable arguments strings are all remaining arguments
strings=("$#")
declare -p count seconds strings
Example usages
With strings not starting with a dash:
$ ./script -t45 -n10 foo bar baz qux
declare -- count="10"
declare -- seconds="45"
declare -a strings=([0]="foo" [1]="bar" [2]="baz" [3]="qux")
With string starting with a dash, need -- delimiter:
$ ./script -t45 -n10 -- '-dashed string' foo bar baz qux
declare -- count="10"
declare -- seconds="45"
declare -a strings=([0]="-dashed string" [1]="foo" [2]="bar" [3]="baz" [4]="qux")
I have written a script that gets a variable number of arguments:
test.sh -i <input1> <input2> ... -o <output1> <output2> ...
I'm parsing the arguments as follows:
while [ $# -gt 1 ]; do
TMP=$(echo "$#" | cut -d '-' -f 2) #i <input1> <input2>
TMP1=$(echo "$TMP" | cut -d ' ' -f 1) #i
CNT=$(echo "$TMP" | wc -w) #3
set -x
case "$TMP1" in
i)
INPUTS=$(echo "$TMP" | cut -c 3-)
shift "$CNT"
;;
o)
OUTPUTS=$(echo "$TMP" | cut -c 3-)
shift "$CNT"
;;
esac
done
This works everytime, except for files that happen to have a '-' in their name.
Example:
./test.sh -i file1.txt file-2.txt -o out1.txt out-2.txt
Is there anyway I can force cut to ignore delimiters that occur within the file names?
You don't need all this string manipulation; each argument is already a separate word.
while (( $# > 0 )); do
case $1 in
-i) shift
while [[ $# -gt 0 && $1 != -* ]]; do
inputs+=( "$1" )
shift
done
;;
-o) shift
while [[ $# -gt 0 && $1 != -* ]]; do
outputs+=( "$1" )
shift
done
;;
*) echo "Unrecognized option $1"
exit 1
;;
esac
done
This can be refactored a little to avoid the repeated checks for running out of arguments.
for arg in "$#"; do
case $1 in
-i) mode=input; continue ;;
-o) mode=output; continue ;;
esac
case $mode in
input) input+=("$arg") ;;
output) output+=("$arg") ;;
*) echo "Unknown mode: $mode"
exit 1
;;
esac
done
Here's an alternative approach that may benefit someone.
The fact is, argument parsing is always a tradeoff, hence there's benefit in tailoring it to the application. Here's a pretty generic solution that allows a little bit of error checking and disorder in the arguments.
It's very simple, but I have added some example output and comments, and for the sake of readability and compatibility, stayed away from complex ways to save a line or two (especially on the if statements).
Sample Usage:
bash #> touch file-1 file3 file4 file-8 file7
bash #> argparse -i file-1 file3 file4 -c -k --q --j -r -t -o file-8 file7
Output:
Input files: file-1 file3 file4
Output files: file-8 file7
Args are: c k q j r t
Doing action for argument "c"
Doing action for argument "k"
Doing action for argument "j"
Script:
#!/bin/bash
#argparse
#Assign arrays
until [[ $# < 1 ]]; do
#ignore args "-i" and "-o", and tell the script to check for files following
if [ "$1" == "-i" ] ; then unset output ; input=1 ; shift
elif [ "$1" == "-o" ] ; then unset input ; output=1 ; shift
fi
#Add input and output files to respective arrays
if [ -f "$1" ] ; then
if [[ $input == 1 ]]; then
infiles+=($1)
elif [[ $output == 1 ]]; then
outfiles+=($1)
fi
else
#Add args to array
arg="$(echo "$1" | sed 's/-//g')"
args+=($arg)
fi
shift
done
#Some debug feedback
echo -e "Input files: ${infiles[#]}\nOutput files: ${outfiles[#]}\nArgs are: ${args[#]}\n"
#Simulate actually "doing" something with the args
for arg in "${args[#]}" ; do
case $arg in
"c") echo "Doing action for argument \"c\"" ;;
"k") echo "Doing action for argument \"k\"" ;;
"j") echo "Doing action for argument \"j\"" ;;
*) ;;
esac
done
Update/Edit: I've just realised, that the OP didn't have any requirement for parsing actual arguments other than -i and -o. Well regardless, this may still come in handy for someone at some point.
I have a small problem here.
I've written a script, which works fine. But there is a small problem.
The script takes 1 or 2 arguments. The 2nd arguments is a .txt file.
If you write something like my_script arg1 test.txt, the script will work. But when you write my_script arg1 < test.txt it doesn't.
Here is a demo of my code:
#!/bin/bash
if [[ $# = 0 || $# > 2 ]]
then
exit 1
elif [[ $# = 1 || $# = 2 ]]
then
#do stuff
if [ ! -z $2 ]
then
IN=$2
else
exit 3
fi
fi
cat $IN
How can I make it work with my_script arg1 < test.txt?
If you just want to change how my_script is called, then just let cat read from myscript's standard input by giving it no argument:
#!/bin/bash
if [[ $# != 0 ]]
then
exit 1
fi
cat
If you want your script to work with either myscript arg1 < test.txt or myscript arg1 test.txt, just check the number of arguments and act accordingly.
#!/bin/bash
case $# in
0) exit 1 ;;
1) cat ;;
2) cat $2 ;;
esac
If you look at how the guys at bashnative implemented their cat you should be able to use 'read' to get the piped content..
eg. do something like this:
while read line; do
echo -n "$line"
done <"${1}"
HTH,
bovako
I am trying to figure out a sane way to do a NOT clause in a case. The reason I am doing this is for
transcoding when a case is met, aka if I hit an avi, there's no reason to turn it into an avi again, I can
just move it out of the way (which is what the range at the base of my case should do). Anyway, I have some
proto code that I wrote out that kind of gives the gist of what I am trying to do.
#!/bin/bash
for i in $(seq 1 3); do
echo "trying: $i"
case $i in
! 1) echo "1" ;; # echo 1 if we aren't 1
! 2) echo "2" ;; # echo 2 if we aren't 2
! 3) echo "3" ;; # echo 3 if we aren't 3
[1-3]*) echo "! $i" ;; # echo 1-3 if we are 1-3
esac
echo -e "\n"
done
expected results would be something like this
2 3 ! 1
1 3 ! 2
1 2 ! 3
Help is appreciated, thanks.
This is contrary to the design of case, which executes only the first match. If you want to execute on multiple matches (and in your design, something which is 3 would want to execute on both 1 and 2), then case is the wrong construct. Use multiple if blocks.
[[ $i = 1 ]] || echo "1"
[[ $i = 2 ]] || echo "2"
[[ $i = 3 ]] || echo "3"
[[ $i = [1-3]* ]] && echo "! $i"
Because case only executes the first match, it only makes sense to have a single "did-not-match" handler; this is what the *) fallthrough is for.
You can do this with the extglob extension.
$ shopt -s extglob
$ case foo in !(bar)) echo hi;; esac
hi
$ case foo in !(foo)) echo hi;; esac
$
My code:
#!/bin/bash
for i in $#;
do echo $i;
done;
run script:
# ./script 1 2 3
1
2
3
So, I want to skip the first argument and get:
# ./script 1 2 3
2
3
Use the offset parameter expansion
#!/bin/bash
for i in "${#:2}"; do
echo $i
done
Example
$ func(){ for i in "${#:2}"; do echo "$i"; done;}; func one two three
two
three
Use shift command:
FIRST_ARG="$1"
shift
REST_ARGS="$#"
Look into Parameter Expansions in the bash manpage.
#/bin/bash
for i in "${#:2}"
do echo $i
done
You could just have a variable testing whether it's the first argument with something like this (untested):
#!/bin/bash
FIRST=1
for i in $#
do
if [ FIRST -eq 1 ]
then
FIRST=0
else
echo $i
fi
done