LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Bash: Is there a more elegant way to write if statement (https://www.linuxquestions.org/questions/linux-software-2/bash-is-there-a-more-elegant-way-to-write-if-statement-4175695499/)

GPGAgent 05-23-2021 07:19 AM

Bash: Is there a more elegant way to write if statement
 
My bash script takes 3,5,7,9 or 11 arguments, a file name and a start and stop point to cut from the file, up to five start and stop points, so this is what I've constructed, is there a more elegant way:
Code:

#!/bin/bash
renice 19 -p $$
if [[ $# -eq 3 ]] || [[ $# -eq 5 ]] || [[ $# -eq 7 ]] || [[ $# -eq 9 ]] || [[ $# -eq 11 ]] ; then
:
else
    echo "====================================================================="
    echo "Please enter the Input file name start point and duration in hh:mm:ss"
    echo "Example: abc.mts 12:00 15:00 i.e. starts at 12 min and ends at 15 min"
    echo "You can have up to 5 cuts from a single file"
    echo "====================================================================="
    exit 1
fi

echo "No of parameters: $#"
echo "Processing file $1"
echo "start at $2"
echo "end at $3"

exit 0

Okay there is no actual processing code, I just want to get the framework of the script done. It works like this,

No Parameters:
Code:

jonke@charlie:~$ HB-Cutter.sh
11316 (process ID) old priority 0, new priority 19
=====================================================================
Please enter the Input file name start point and duration in hh:mm:ss
Example: abc.mts 12:00 15:00 i.e. starts at 12 min and ends at 15 min
You can have up to 5 cuts from a single file.
=====================================================================
jonke@charlie:~$

Three arguments:
Code:

jonke@charlie:~$ HB-Cutter.sh abc.mp4 12:24 15:30
11321 (process ID) old priority 0, new priority 19
No of parameters: 3
Processing file abc.mp4
start at 12:24
end at 15:30
jonke@charlie:~$

Four arguments:
Code:

jonke@charlie:~$ HB-Cutter.sh abc.mp4 12:24 15:30 18:20
11335 (process ID) old priority 0, new priority 19
=====================================================================
Please enter the Input file name start point and duration in hh:mm:ss
Example: abc.mts 12:00 15:00 i.e. starts at 12 min and ends at 15 min
You can have up to 5 cuts from a single file
=====================================================================
jonke@charlie:~$

Five arguments:
Code:

jonke@charlie:~$ HB-Cutter.sh abc.mp4 12:24 15:30 18:20 20:36
12065 (process ID) old priority 0, new priority 19
No of parameters: 5
Processing file abc.mp4
start at 12:24
end at 15:30
jonke@charlie:~$

and yes, I've not printed the 4th and 5th argument, but when finished the script will process them

michaelk 05-23-2021 08:33 AM

There are many ways... getopts is the bash builtin command to parse command line options.

Code:

#!/bin/bash
declare -a start
declare -a duration

a=$#
b=$((a-1))
n=$((b%2))

if [[ $n != 0 || $a -gt 11 ]]; then
  echo 'invalid argument list'
  #......
  exit
fi

shift 1
i=$((b/2))
for (( c=1; c<=i; c++))
do
    echo "$1,$2"
    start+=($1)
    duration+=($2)
    shift 2
done
echo ${start[@]}
echo ${duration[@]}

Not sure about more elegant but easier to manage number of time segments.

lvm_ 05-23-2021 08:33 AM

Hope this snipped will give you some pointers

Code:

echo file $1
shift
while [ $# != 0 ]; do
  echo start $1
  echo end $2
  shift 2
done


GPGAgent 05-23-2021 08:37 AM

Thanks guys, I've used getopts in other scripts, just trying something different, but it's a good suggestion as is the shift method.

I think I might go down the shift method, that'll allow me any number of cuts.

Note: I'm a product of the 1970's, cut my teeth on Fortran IV on IBM 360 machines.....

"say no more" as they said in Monty Python!

rnturn 05-23-2021 02:05 PM

Quote:

Originally Posted by GPGAgent (Post 6253185)
My bash script takes 3,5,7,9 or 11 arguments, a file name and a start and stop point to cut from the file, up to five start and stop points, so this is what I've constructed, is there a more elegant way:
Code:

#!/bin/bash
renice 19 -p $$
if [[ $# -eq 3 ]] || [[ $# -eq 5 ]] || [[ $# -eq 7 ]] || [[ $# -eq 9 ]] || [[ $# -eq 11 ]] ; then
:
else

<snip>


Instead of a gory if-then, there's case-esac:
Code:

#!/bin/bash

NARGS=$#

case ${NARGS} in
    3 | 5 | 7 | 11)
        FILE=$1; shift
        while [ $# -gt 0 ]
        do
            START=$1; shift
            STOP=$1; shift
        #  Act on $FILE
        done
        ;;
    *)
        echo "Wrong number of args."
        exit 1
esac

HTH...

MadeInGermany 05-23-2021 03:37 PM

If one branch exits then you can leave the other branch empty.
Code:

case ${NARGS} in
( 3 | 5 | 7 | 11 )
;;
( * )
    echo "Wrong number of args."
    exit 1
esac
FILE=$1; shift
while [ $# -gt 0 ]
do
    START=$1; shift
    STOP=$1; shift
#  Act on $FILE
done


MadeInGermany 05-23-2021 03:48 PM

A tricky alternative to the case-esac is
Code:

if [[ " 3 5 7 9 11 " != *" ${NARGS} "* ]]
then
    echo "Wrong number of args."
    exit 1
fi



All times are GMT -5. The time now is 05:12 PM.