[SOLVED] Bash: Is there a more elegant way to write if statement
Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
Distribution: openSUSE, Raspbian, Slackware. Older: Coherent, MacOS, Red Hat, Big Iron IXs: AIX, Solaris, Tru64
Posts: 2,741
Rep:
Quote:
Originally Posted by GPGAgent
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.