LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-23-2021, 07:19 AM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
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

Last edited by GPGAgent; 05-23-2021 at 08:12 AM.
 
Old 05-23-2021, 08:33 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,698

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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.

Last edited by michaelk; 05-23-2021 at 08:38 AM.
 
1 members found this post helpful.
Old 05-23-2021, 08:33 AM   #3
lvm_
Member
 
Registered: Jul 2020
Posts: 923

Rep: Reputation: 336Reputation: 336Reputation: 336Reputation: 336
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
 
1 members found this post helpful.
Old 05-23-2021, 08:37 AM   #4
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
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!

Last edited by GPGAgent; 05-23-2021 at 08:40 AM.
 
Old 05-23-2021, 02:05 PM   #5
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by GPGAgent View Post
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...
 
1 members found this post helpful.
Old 05-23-2021, 03:37 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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
 
1 members found this post helpful.
Old 05-23-2021, 03:48 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Elegant way to store realtime data on a USB key? displace Linux - General 10 02-25-2013 08:23 AM
Whats the most elegant way of comparing different data types mreff555 Programming 6 08-18-2012 02:29 PM
Most elegant way to back up /home TwinReverb Linux - General 2 12-25-2008 12:44 PM
Total size of installed package - more elegant way to get it? ErV Slackware 2 11-19-2008 02:52 PM
New Kernel and Alsa - an elegant way? Misel Slackware 5 10-30-2003 12:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration