LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-02-2018, 10:12 AM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Rep: Reputation: 56
Question regarding getopts...


Hello

I want to use options in my script and want to execute like this.

Code:
myscript -o a input.file output.file
Where -o is some mandatory option.

Following is my script:
Code:
#!/bin/bash

ShowHelp() {
  echo "Some help"
}

MyFunction() {
  echo "\$1 = $1"
  echo "\$2 = $2"
  echo "\$3 = $3"
}

while getopts ":ho:" opt ; do
  case $opt in
    h)  
      ShowHelp
      ;;  
    o)  
      MyFunction "$OPTARG"
      ;;  
    \?) 
      echo "Invalid option: -$OPTARG"
      ;;  
    :)  
      echo "Option -$OPTARG requires an argument."
      exit 1
      ;;  
  esac
done
These are the results:
Code:
$ test.sh -a
Invalid option: -a

$ test.sh -o
Option -o requires an argument.

$ test.sh -o a
$1 = a
$2 = 
$3 = 

$ test.sh -oa
$1 = a
$2 = 
$3 =
So far so good. But if I add $2 (input.file) and $3 (output.file) , then results will be strange.

Changes in my script:
Code:
.
.

    o)  
      MyFunction "$OPTARG" "$2" "$3"
.
.
Result:
Code:
$ test.sh -oa input.file output.file
$1 = a
$2 = input.file
$3 = output.file

$ test.sh -o a input.file output.file
$1 = a
$2 = a
$3 = input.file
As you can see from the results, $1 will always be a, doesn't matter there is a space in between option or not. But $2 and $3 will have different values based on that space between the option and its argument. I don't want this to happen.

I want $1 $2 and $3 to hold same values irrespective of there is space or not in between them.

Code:
$ test.sh -oa input.file output.file
$ test.sh -o a input.file output.file
..be same.

How to do it?

Any help really appreciated.

Thanks
 
Old 09-02-2018, 11:08 AM   #2
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
Your input and output files parameters are script parameters and not arguments of -o option.

So, you have to manage all your options and launch your function after.
Code:
#!/bin/bash

ShowHelp() {
  echo "Some help"
}

MyFunction() {
  echo "\$1 = $1"
  echo "\$2 = $2"
  echo "\$3 = $3"
}

while getopts ":ho:" opt ; do
  case $opt in
    h)
      ShowHelp
      ;;
    o)
      Oarg=$OPTARG
      ;;
    \?)
      echo "Invalid option: -$OPTARG"
      ;;
    :)
      echo "Option -$OPTARG requires an argument."
      exit 1
      ;;
  esac
done
shift $((OPTIND-1))

MyFunction "$Oarg" "$1" "$2"
Code:
bash toto -h -oa Un Deux
And
Code:
bash toto -h -o a Un Deux
Returns :
Code:
Some help
$1 = a
$2 = Un
$3 = Deux
 
1 members found this post helpful.
Old 09-02-2018, 11:35 AM   #3
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by lougavulin View Post
Your input and output files parameters are script parameters and not arguments of -o option.

So, you have to manage all your options and launch your function after.
Code:
#!/bin/bash

ShowHelp() {
  echo "Some help"
}

MyFunction() {
  echo "\$1 = $1"
  echo "\$2 = $2"
  echo "\$3 = $3"
}

while getopts ":ho:" opt ; do
  case $opt in
    h)
      ShowHelp
      ;;
    o)
      Oarg=$OPTARG
      ;;
    \?)
      echo "Invalid option: -$OPTARG"
      ;;
    :)
      echo "Option -$OPTARG requires an argument."
      exit 1
      ;;
  esac
done
shift $((OPTIND-1))

MyFunction "$Oarg" "$1" "$2"
Code:
bash toto -h -oa Un Deux
And
Code:
bash toto -h -o a Un Deux
Returns :
Code:
Some help
$1 = a
$2 = Un
$3 = Deux
Oh! Now I understand.

Is this right way then,

Code:
#!/bin/bash

ShowHelp() {
  echo "Some help"
}

MyFunction() {
  echo "\$1 = $1"
  echo "\$2 = $2"
  echo "\$3 = $3"
}

while getopts ":ho:" opt ; do
  case $opt in
    h)  
      ShowHelp
      ;;  
    o)  
      ARG="$OPTARG"
      ;;  
    \?) 
      echo "Invalid option: -$OPTARG"
      ;;  
    :)  
      echo "Option -$OPTARG requires an argument."
      exit 1
      ;;  
  esac
done
shift $((OPTIND-1))

if [[ $ARG == "a" ]] ; then
  MyFunction "$ARG" "$1" "$2"

elif [[ $ARG == "b" ]] ; then
  MyFunction "$ARG" "$1" "$2"

else
  echo "Argument should be 'a' or 'b'"
fi
 
Old 09-02-2018, 12:54 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
It doesn't work like you think it does.

getopt finds the argument of the option regardless if there is a space between them i.e -oa or -o a.

The difference between -oa and -o a is that bash considers -oa as one command line argument versus -o a as two. I Will ignore the case of using quotes in command line arguments...

$1 will always be the first command line argument regardless. getops will stop when it can not find any more hyphens (-).

To make things simple I might suggest using an option for the input file and assume that the last command line argument is the output file.
 
1 members found this post helpful.
Old 09-02-2018, 01:01 PM   #5
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
Quote:
Originally Posted by michaelk View Post
To make things simple I might suggest using an option for the input file and assume that the last command line argument is the output file.
Yes, that would be better and according how many options OP wants for his script, that will help a lot.
 
1 members found this post helpful.
Old 09-02-2018, 02:50 PM   #6
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
While in the script I'm writing there will be several options, but only one option will be used at any given time. That could be with no argument or one argument.

Something like this:

Code:
script -h                      # Show help

script -l {a,h,x}              # -la => show all video files. 
                                 -lh => show video files with hevc codec. 
                                 -lx => show video files which are not hevc

script -c {n,h,f} input output # -cn => Convert, Normal, dont change resolution, 
                                 -ch => Convert to HD 
                                 -cf => Convert to Full HD

script -r {n,h,f}              # Resursively convert all videos and replace with same name in a directory.
                                 Arguments same as above.
 
Old 09-02-2018, 03:43 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
shift $((OPTIND-1))

The above command basically will shift the position of the cli arguments so now $1 will be the first remaining argument that isn't an option, $2 the next. You can parse them for your input and output file names.
 
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
getopts question in bash script. lleb Linux - Newbie 4 07-20-2012 05:37 PM
Need Help with getopts paragkalra Programming 4 03-26-2011 09:17 PM
getopts kj6loh Programming 4 09-10-2009 03:53 PM
question on getopts tostay2003 Programming 1 12-25-2007 04:39 AM
Help with getopts command Rezon Programming 3 10-22-2003 04:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:33 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