0

I'm trying to implement a help message on my shell script to get a usage example when running my script with "-h" as a parameter. I looked for ways to do it on-line but it's just not working...

This is my code:

#!/usr/bin/env sh

usage="$(basename "$0") [-h] -- program to change all files from one extension to another extension

where:
    -h      show this help text
    ext1    extension to be changed
    ext2    new desired extension

    usage example: ./chage_entension.sh .cpp .c"

while [[ getopts ':hs:' option ]]; do
    case "$option" in
        h) echo "$usage"
            ;;
    esac
done
shift $((OPTIND - 1))

ext1 = "$1"
ext2 = "$2"

for file in *$1; do
    mv "$file" "`basename "$file" $1`$2"
done

And this are the errors I'm getting when trying to run ./change_extension.sh

/home/andre/src/scripts/change_extension.sh: line 12: conditional binary operator expected
/home/andre/src/scripts/change_extension.sh: line 12: syntax error near `':hs:''
/home/andre/src/scripts/change_extension.sh: line 12: `while [[ getopts ':hs:' option ]]; do'
  • `[[ ]]` is not a generic piece of syntax for "if conditions go here". It has a specific meaning, and putting a `getopts` call inside it doesn't make sense in conjunction with that meaning. – Charles Duffy Mar 31 '17 at 23:27
  • Also, `[[` is *never* safe to use with `/bin/sh`; it's a ksh extension also available in bash, but not part of the POSIX shell command language specification, which is all the `#!/bin/sh` shebang guarantees to make available. – Charles Duffy Mar 31 '17 at 23:27
  • There are other syntax issues here as well; http://shellcheck.net/ will catch several. – Charles Duffy Mar 31 '17 at 23:28
  • (`#!/bin/sh` isn't `zsh` either; no idea what the tag is doing on this question). – Charles Duffy Mar 31 '17 at 23:30
  • Thank you for the tips @CharlesDuffy ! This is really just my third shell script. I used shellcheck.net and it's now working as I wanted! – Andre Korol Mar 31 '17 at 23:38

0 Answers0