linux - issue in getopt , Unix shell script -


Hi anyone can fix this problem, I'm not able to outpt.

I'm not able to get the output of P

  #! / Bin / bash args = `getopt c: m: p $ *` if [$? ! = 0 -o $ # == 0] Then use 'echo': -C & lt; Current-diamond & gt; -M & LT; My Dior & gt; -P & lt; Logic & gt; 'Exit 1fi set- I change' $ i '-c for $ args; CURRDIR = $ 1; Shift; Shift ;; -M) MYDIR = $ 1; Shift ;; -p) ARGVAL = $ OPTARG ;; ASAC resonates "curved = $ crowder" echo "echo" = "$ MYDIR" "ARGVL = $ ARGVAL" ./1.sh -c "def" -m "referee" -p "riff-of-referee"  

Expected output

  output -c = "def" -m = "ref" -p = "ref -k ref"  

getopt

  args = `getopt c: m: p $ *` << / Code> 

To take an indication of the fact that -p takes an argument, after adding a colon to p . Apart from this, you should change from $ * to "$ @" for better management of spaces.

  args = `getopt c: m: p:" $ @ "`  

You are also traveling and traveling $ OPTARG has a getopts feature with plain cutoff and you should just use $ 2 and then close the argument.

  -p) ARGVAL = $ 2; Shift 2 ;;  

At this point you have done as good as you can with getopt Unfortunately it does not handle multi-word logic with -p What you do, whatever you do. For this, we need to use the doufs.

getopts

from:

Easy and easy to use, usually better, though not available at all in the CSS-like shell They should not even use them.

This work is different from "Diopette" first, because it is a built-in, you will not usually find a separate man page for it, though "can get help" to give you that thing do you want it.

The old "doop" is called once, and it modifies the environment as we have seen. Each time you want to process an argument, the built "does" is called, and it does not change the original logic.

It is very easy to use the goots, your entire loop can be simplified on:

  while getopts c: m: p: flag " $ Flags "c) CURRDIR = $ OPTARG ;; I) MYDIR = $ OPTARG ;; P) ARGVAL = $ OPTARG ;; SAC  

No transfer is needed, you just read $ OPTARG to get the value of every option.


Comments