Loop Script throws 0 by executing touch - bash

I'm writing a script that makes a while loop for me, that I doesn't have to write a one line while loop again and again.
Short explanation:
The command "loop" will execute the command parameter -n times and with -p seconds pause. You can use -e to echo the command and not execute it. -v to show all parameters. Additionally its possible to use the iterator of the while loop.
Playing around with it, I've found a bug.
loop --verbose --times 5 'touch testfile$i'
#loop the touch command 5 times and generate the testfiles 0 to 4
same as:
loop -v -n 5 'touch testfile$i'
It generate "testfile0" to "testfile4" but also a file named "0"
Do somebody know why the file "0" will be generated?
And what do you think about a command like that? Would you like to use it?
Best
Steven
#!/bin/bash
# easy to use loop command
#
# Autor: Steven Wagner
# Date: 22 June 2018
# Version: 0.1
TIMES=0
FREQUENCY=0
PAUSE="0"
VERBOSE=false
ECHOONLY=false
SILENCE=false
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-n|--times)
temp="$2"
if [[ $temp =~ ^[[:digit:]]+$ ]];
then
TIMES=$temp
else
echo "ERROR: value is not a digit"
exit
fi
shift # past argument
shift # past value
;;
-p|--pause)
#PAUSE="$2"
temp="$2"
PAUSE=$temp
#if [[ $temp =~ ^[[:digit:]]+$ ]];
#then
# PAUSE=$temp
#else
# echo "ERROR: value is not a digit"
# exit
#fi
shift # past argument
shift # past value
;;
-v|--verbose)
VERBOSE=true
shift # past argument
;;
-e|--echo-only)
ECHOONLY=true
shift # past argument
;;
-s|--silence)
SILENCE=true
shift # past argument
;;
--*)
echo "Error \"$key\" not known"
shift #past argument
;;
-*)
i=0
while [[ $i -lt ${#key}-1 ]]
do
char=${key:$[$i+1]:1}
case $char in
v)
VERBOSE=true
;;
e)
ECHOONLY=true
;;
s)
SILENCE=true
;;
esac
(( i++ ))
done
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[#]}" # restore positional parameters
#Pause make sleep command
pause=""
if [ $PAUSE > 0 ]
then
pause="sleep $PAUSE"
fi
#times make while control-command
times=""
if [ $TIMES != 0 ]
then
times="[ \$i -lt \$TIMES ]"
else
times="true" #infinity loop
fi
#Echo Only
command=$#
if $ECHOONLY
then
command="echo $#"
fi
#Silence
silence=""
if $SILENCE && ! $ECHOONLY
then
silence="&>/dev/null"
fi
if $VERBOSE
then
echo "pause = $PAUSE"
if [ $TIMES != 0 ]
then
echo "times = $TIMES"
else
echo "times = infinity"
fi
if $ECHOONLY; then echo "echo only = yes"; fi
if $SILENCE; then echo "silence = yes"; fi
echo "command = '$#' $silence"
echo
fi
#start the loop
timestamp=$(date +%s%N)
eval "
i=0
while $times
do
eval '$command' $silence
$pause
(( i++ ))
done
"
if $VERBOSE
then
echo
echo "duration: $[($(date +%s%N)-$timestamp)/1000000]ms"
fi

Related

How to pass a long option to a bash script?

./script.sh -abc hello
How can I write my script to use '-abc' as the option and 'hello' as the value to that option?
I should be able to pass this value to all the functions in this script. Lets say I have 2 functions: X and Y.
Use this in your script:
[[ $1 == -abc ]] && value="$2" || echo invalid option
If you don't want to print any messages on wrong option or no option, then omit the || echo ... part, value will be empty.
If you want to make the second argument a must, then:
[[ $1 == -abc ]] && [[ $2 != "" ]] && value="$2" || echo invalid option
Using if else loop will give you complete control over this:
if [[ $1 == -abc ]]; then
#if first option is valid then do something here
if [[ $2 != "" ]]; then
value="$2"
else
#if second option is not given then do something here
echo invalid option
fi
else
echo invalid option
#if first option is invalid then do something here
fi
If you want to make the first argument a must too, then change the first if statement line to
if [[ $1 == -abc && $1 != "" ]]; then
If you want to pass as many arguments as you wish and process them,
then use something like this:
#!/bin/bash
opts=( "$#" )
#if no argument is passed this for loop will be skipped
for ((i=0;i<$#;i++));do
case "${opts[$i]}" in
-abc)
# "${opts[$((i+1))]}" is the immediately follwing option
[[ "${opts[$((i+1))]}" != "" ]] &&
value="${opts[$((i+1))]}"
echo "$value"
((i++))
#skips the nex adjacent argument as it is already taken
;;
-h)
#dummy help option
echo "Options are [-abc value], -h"
;;
*)
#other unknown options
echo invalid option
break
;;
esac
done
This is an example of handling multiple arguments with only two options available -abc value and -h
bash doesn't have a built in command for processing long arguments. In order to parse long options in a shell script, you'll need to iterate over the arguments list yourself.
Here's one approach:
#!/bin/sh
is_option_arg () {
case $1 in
-*)
return 1
;;
*)
return 0
;;
esac
}
usage () {
echo "$(basename "$0") -abc ARG -def ARG -verbose"
}
OPT_ABC=
OPT_DEF=
OPT_VERBOSE=false
while [ "$#" -gt 0 ]; do
case $1 in
-abc)
shift
{ [ "$#" -ne 0 ] && is_option_arg "$1"; } || { usage >&2; exit 1; }
OPT_ABC=$1
;;
-def)
shift
{ [ "$#" -ne 0 ] && is_option_arg "$1"; } || { usage >&2; exit 1; }
OPT_DEF=$1
;;
-verbose)
OPT_VERBOSE=true
;;
*)
break
;;
esac
shift
done
echo "OPT_ABC=$OPT_ABC"
echo "OPT_DEF=$OPT_DEF"
echo "OPT_VERBOSE=$OPT_VERBOSE"
if [ "$#" -gt 0 ]; then
echo "Remaining args:"
for arg in "$#"; do
echo "$arg"
done
fi
You pretty much have to implement it yourself manually. Here's one way:
abc=
while [[ "$1" == -* ]]; do
opt=$1
shift
case "$opt" in
-abc)
if (( ! $# )); then
echo >&2 "$0: option $opt requires an argument."
exit 1
fi
abc="$1"
shift
;;
*)
echo >&2 "$0: unrecognized option $opt."
exit 2
;;
esac
done
echo "abc is '$abc', remaining args: $*"
Some sample runs of the above:
(0)$ ./script.sh
abc is '', remaining args:
(0)$ ./script.sh hello
abc is '', remaining args: hello
(0)$ ./script.sh -abc hello
abc is 'hello', remaining args:
(0)$ ./script.sh -abc hello there
abc is 'hello', remaining args: there
(0)$ ./script.sh -abc
./script.sh: option -abc requires an argument.
(1)$ ./script.sh -bcd
./script.sh: unrecognized option -bcd.
(2)$

Testing truth/falseness in bash script

For some reason, I can't figure out how to test truth in bash:
#!/bin/bash
FORCE_DELETE=""
BE_VERBOSE=""
OPTIND=1
while getopts ":fv" FLAG "$#" ; do
if [[ "$FLAG" == "f" ]] ; then
FORCE_DELETE="true"
fi
if [[ "$VALUE" == "v" ]] ; then
BE_VERBOSE="true"
fi
if [[ "$FLAG" == "?" ]] ; then
echo "Usage: $0 [-fv] file ..."
exit 1
fi
done
shift `expr $OPTIND - 1`
if [[ "$FORCE_DELETE" == "true" && "BE_VERBOSE" == "true" ]] ; then
echo "FORCE_DELETE AND BE_VERBOSE $#"
elif [[ "$FORCE_DELETE" == "true" ]] ; then
echo "FORCE_DELETE $#"
elif [[ "$BE_VERBOSE" == "true" ]] ; then
echo "BE_VERBOSE $#"
else
echo "$#"
fi
exit 0
Transcript:
$ test a b
a b
$ test -f a b
FORCE_DELETE a b
$ test -v a b
a b
$ test -fv a b
FORCE_DELETE a b
Why does my bash script respond to the -f flag but not the -v flag?
Most likely a typo :
[[ "$VALUE" == "v" ]],
this should be
[[ "$FLAG" == "v" ]]
You specifically ask about testing true/false. These are built in to the language rather than using strings, and you don't need the [[ test. Here is how I would write this:
#!/bin/bash
force_delete=false # Don't use UPPERCASE
be_verbose=false # they could collide with reserved variables
# OPTIND does not need to be initialised
while getopts :fv flag
do
# appears one of your if statements is incorrect
# a case is often used with getopts
case $flag in
f) force_delete=true
;;
v) be_verbose=true
;;
\?) echo "Usage: $0 [-fv] file ..."
exit 1
;;
esac
done
shift $((OPTIND-1)) # don't create a child process for simple arithmetic
if $force_delete && $be_verbose
then
echo "force_delete AND be_verbose $#"
elif $force_delete
then
echo "force_delete $#"
elif $be_verbose
then
echo "be_verbose $#"
else
echo "$#"
fi
# Bash exits 0 by default

Bash using getopts doesn't return expected echo value

I'm attempting to check if one of two flags exists. Either -c or -v.
However, on attempting to run the below I'm finding that the script exits without outputting anything.
I've had a look at changing the position of the colon in the while getopts “:v:” opts; do statement and this appears to change nothing.
Any ideas?
#Check that the $ACTION variable is of the form “-[character]"
if [[ ! $ACTION =~ ^-. ]] ; then
echo "2";
printHelp;
exit 1;
fi
#Too many arguments passed
if [[ ${#ACTION} -gt 2 ]] ; then
echo "3";
printHelp;
exit 1;
fi
#Use cases for correct characters
while getopts “:v:” opts; do
echo "4";
case $opts in
v) echo "1234" #vocabTest;
echo "${bold}STARTING VOCAB TEST:${reset}"
break
;;
c) #phraseTest;
echo "${bold}STARTING PHRASE TEST:${reset}"
exit 0
;;
?) echo "";
printHelp
exit 1
;;
esac
done
}
I removed the entire while loop as it was not serving any purpose. The below ended up working:
##########################################################################################
# Check args passed to the script #
##########################################################################################
ACTION=$1;
#Check that the $ACTION variable is of the form “-[character]"
if [[ ! $ACTION =~ ^-. ]] ; then
printHelp;
exit 1;
fi
#Too many arguments passed
if [[ ${#ACTION} -gt 2 ]] ; then
printHelp;
exit 1;
fi
#Use cases for correct characters
case ${ACTION} in
(-v) echo "${bold}STARTING VOCAB TEST:${reset}"
;;
c) #phraseTest;
echo "${bold}STARTING PHRASE TEST:${reset}"
exit 0
;;
?) echo "";
printHelp
#exit 1
;;
esac

Getopts in bash not selecting option

I have one problem , when i select one option , for exemple ./test.sh -f it should print "mel" but it reads all code.
How does it enter the if condition and passes with other argument ?
if getopts :f:d:c:v: arg ; then
if [[ "${arg}" == d ]] ; then
d_ID=$OPTARG
eval d_SIZE=\$$OPTIND
else
echo "Option -d argument missing: needs 2 args"
echo "Please enter two args: <arg1> <arg2>"
read d_ID d_SIZE
echo "disc $d_ID $d_SIZE" >> $FILENAME
fi
if [[ "${arg}" == c ]] ; then
c_NOME="$OPTARG"
eval c_ID1=\$$OPTIND
eval c_ID2=\$$OPTINDplus1
eval c_FICHEIRO=\$$OPTINDplus2
else
echo "Option -c argument missing: needs 4 args"
echo "Please enter two args: <arg1> <arg2> <arg3> <agr4>"
read c_NOME c_ID1 c_ID2 c_FICHEIRO
echo "raidvss $c_NOME $c_ID1 $c_ID2 $c_FICHEIRO" >> $FILENAME
fi
if [[ "${arg}" == f ]] ; then
echo "mel"
fi
fi
You are using getopts parameters wrong.
if getopts :f:d:c:v: arg
means that -f will follow the value of parameter, like
-f 5
If you want just have -f (without value) you need to change it to
if getopts :fd:c:v: arg ; then
(I deleted the ':'). Also, I think you should better use while cycle and case statements.
See this example
while getopts fd:c:v: opt
do
case "$opt" in
f) echo "mel";;
d) discFunction "$OPTARG";;
c) otherFunction "$OPTARG";;
v) nop;;
\?) echo "$USAGE" >&2; exit 2;;
esac
done
shift `expr $OPTIND - 1`

comparing $var to

I have a test script the needs to read the variable 'LAB' and e-mail the correct company.
I've looked but can't find anything that has worked yet.
Any thoughts?
#!
#
LAB=3
#
if [ "$LAB" = "$1" ];then
echo "Got Zumbrota" && ./mailZ
fi
#
if [ "$LAB" = "$2" ];then
echo "Got Barron" && ./mailB
fi
#
if [ "$LAB" = "$3" ];then
echo "Got Stearns" && ./mailS
fi
If this a bash script, start your file with
#!/bin/bash
and use -eq for integer comparison and since LAB is an integer in your script
if [ $LAB -eq $1 ]
These cascading if statements can be condensed into a case statement:
case "$LAB" in
1) echo "Got Zumbrota" && ./mailZ
;;
2) echo "Got Barron" && ./mailB
;;
3) echo "Got Stearns" && ./mailS
;;
*) echo "don't know what to do with $LAB"
;;
esac

Resources