Picking script options in bash

I would like to select a particular file by passing an option to a bash script so that I can call another
program to compile an outline of some documents I have

But I am getting an error when running the script, for example

./outlineCh.sh --Segpi
./outlineCh.sh: line 154: syntax error: unexpected end of file
#!/bin/bash

# ----------------------------------------------------------------------

# READ ARGUMENTS

iarg=0
narg="$#"

OLDIFS="$IFS"  # Save IFS (splits arguments on whitespace by default)
IFS="|="       # Split arguments on "|" and "="
set -- $*      # Set positional parameters to command line arguments
IFS="$OLDIFS"  # Set original IFS

while [ "$#" -gt 0 ]; do

  iarg=$(( iarg + 1 ))

  case "$1" in

    # Sets the image density
    "-Capav"|"--Capav"|"Ch02a--Capav")
      arg_Capav="enabled"
      ;;

    # Sets the image density
    "-Igmfn"|"--Igmfn"|"Ch03a--Igmfn")
      arg_Igmfn="enabled"
      ;;

    # Sets the image density
    "-Segpi"|"--Segpi"|"Ch04a--Segpi")
      arg_Segpi="enabled"
      ;;

    # Sets the image density
    "-Exads"|"--Exads"|"Ch05a--Exads")
      arg_Exads="enabled"
      ;;

    # Sets the image density
    "-Isemi"|"--Isemi"|"Ch06a--Isemi")
      arg_Isemi="enabled"
      ;;

    *)
      arg_file="$arg_file ${1}"
      ;;

  esac

  # Skip ahead to the next argument
  shift 1

done

crPath="/home/hagbard/chaos/resip/critical"
dvPath="resources/reports/tdr/Development/tdr--1.0"
txPath="${crPath}/${dvPath}/tx411"

# * [Part I]
flnm01a="Ch01a--Smirs--SmInterdspl--Strategy.texi"
flnm02a="Ch02a--Capav--CnptApl--Ambiv.texi"
flnm03a="Ch03a--Igmfn--Invit--GMtkFndn.texi"

# * [Part II]
flnm04a="Ch04a--Segpi--SgExtrcn--GPhy.texi"
flnm05a="Ch05a--Exads--Exmn--AdmSys.texi"

# * [Part III]
flnm06a="Ch06a--Isemi--ImprSgExtr--GMtk.texi"
flnm07a="Ch07a--Cocpl--COrgn--CplPipl.texi"

if [ -z "${arg_Smirs+X}" ]; then
  flnm="${txPath}/${flnm01a}"
elif if [ -z "${arg_Capav+X}" ]; then
  flnm="${txPath}/${flnm02a}"
elif if [ -z "${arg_Igmfn+X}" ]; then
  flnm="${txPath}/${flnm03a}"
elif if [ -z "${arg_Segpi+X}" ]; then
  flnm="${txPath}/${flnm04a}"
elif if [ -z "${arg_Exads+X}" ]; then
  flnm="${txPath}/${flnm05a}"
elif if [ -z "${arg_Isemi+X}" ]; then
  flnm="${txPath}/${flnm06a}"
elif if [ -z "${arg_Cocpl+X}" ]; then
  flnm="${txPath}/${flnm07a}"
elif [ -z "${arg_file+X}" ]; then
  flnm="${txPath}/${arg_file}"
else
  flnm="${txPath}/${flnm04a}"
fi

exit


I'd start by removing the leading space from the first line: #!/bin/bash - there should be no leading space before the #
Also there're 94 lines in the code sample you gave and the complain is for line 154...

If I put the exit before the if section, I do not get the error. And the script works when I remove the if section for options.

what is this supposed to do? elif [ ! -"${arg_file+X}" ]; then
also don't forget the comment about #!/bin/bash - otherwise it's not a bash script

The space when calling bash was a mistake when pasting the code. I have fixed the -z problem. Small mistake when
I was trying various things to try to get things to work out.

so you're good to go?

No. I fixed the -z but still getting the error. The error line it complains about is a blank line at the end of the script.

  • try changing to this block of code:
if [ -z "${arg_Smirs+X}" ]; then
flnm="${txPath}/${flnm01a}"
elif [ -z "${arg_Capav+X}" ]; then
flnm="${txPath}/${flnm02a}"
elif [ -z "${arg_Igmfn+X}" ]; then
flnm="${txPath}/${flnm03a}"
elif [ -z "${arg_Segpi+X}" ]; then
flnm="${txPath}/${flnm04a}"
elif [ -z "${arg_Exads+X}" ]; then
flnm="${txPath}/${flnm05a}"
elif [ -z "${arg_Isemi+X}" ]; then
flnm="${txPath}/${flnm06a}"
elif [ -z "${arg_Cocpl+X}" ]; then
flnm="${txPath}/${flnm07a}"
elif [ -z "${arg_file+X}" ]; then
flnm="${txPath}/${arg_file}"
else
flnm="${txPath}/${flnm04a}"
fi

I want to check if the option is set so I can then assign the variable flnm .

But I do not know how I can do it. I expected the -z expressions to work.
--- Post updated at 12:31 AM ---

I have also tried the using -v and removed all the code after the if statement. Still same problem.

if [[ -v arg_Smirs ]]; then
  flnm="${txPath}/${flnm01a}"
elif if [[ -v arg_Capav ]]; then
  flnm="${txPath}/${flnm02a}"
elif if [[ -v arg_Igmfn ]]; then
  flnm="${txPath}/${flnm03a}"
elif if [[ -v arg_Segpi ]]; then
  flnm="${txPath}/${flnm04a}"
elif if [[ -v arg_Exads ]]; then
  flnm="${txPath}/${flnm05a}"
elif if [[ -v arg_Isemi ]]; then
  flnm="${txPath}/${flnm06a}"
elif if [[ -v arg_Cocpl ]]; then
  flnm="${txPath}/${flnm07a}"
elif [[ -v arg_file ]]; then
  flnm="${txPath}/${arg_file}"
else
  flnm="${txPath}/${flnm04a}"
fi

--- Post updated at 12:56 AM ---

Solved.

elif if

Could not see it for some reason !!!
--- pdated at 01:06 AM ---

I am using [ -v arg ]; and I am wondering whether I would be better off with [[ -v arg ]];

Perhaps not, as I do not need globbing for checking existence of variables

if [ -v arg_Smirs ]; then
  flnm="${txPath}/${flnm01a}"
1 Like