0

Here is my short script:

#!/bin/bash
user=""
secret=""
pkgname=""

while getopts ":p:s:u:" opt; do
  case ${opt} in
        p )
        pkgname=$OPTARG
        echo "p=$pkgname"
     ;;
      s )
      secret=$OPTARG
        echo "s=$secret"
      ;;
   u )
      user=$OPTARG
        echo "u=$user"
      ;;
   \? )
        usage
        exit 1
      ;;
    :)
    echo "Option -$OPTARG requires an argument."
    usage
    exit 1
  esac
done

When I run it using following command:

./script.sh -u 'uuuu' -s 'aaaa' -p pkg.rpm

Output:

u=uuuu
s=aaaa -p

While I expect it to be

u=uuuu
s=aaaa
p=pkg.rpm

I tried doing multiple combinations of the parameters by changing the sequence as well but not able to solve it. Why the value for 's' is incorrect?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
aaa
  • 415
  • 6
  • 15
  • 3
    I can't reproduce this, your script works as intended for me. – Benjamin W. Aug 30 '19 at 13:26
  • 2
    Works for me. It appears you have a control character before the -p which the CLI parser is not recognized as space. Update your echo link with 'echo "u=$user" | cat -v' The -v on cat will displace non visible control characters. – WaltDe Aug 30 '19 at 16:25
  • Yes there was a ctrl character. Removing it solved the problem. Thanks a lot @WaltDe . – aaa Sep 03 '19 at 06:22

0 Answers0