Bash if statements in one line

I have the following bash code, and wondering if it is possible (and a good idea) to write the if statements in one line.

# Run raytrac on the sound speed model.
if [ $hasArgumentRytr -eq 1 ]; then
  $bashPath/raytrac.bash -fcmd=$fcmd.cmd
fi

# Plot the travel times and ray paths.
if [ $hasArgumentPlotryxt -eq 1 ]; then
  $bashPath/plotryxt.bash -fry=$fcmd.ry -fxt=$fcmd.xt
fi

# Remove source-receiver data exceeding dsrmx, Mm.
if [ $hasArgumentRmsrdist -eq 1 ]; then
  $bashPath/removeSRDist.bash -fry=$fcmd.ry -fxt=$fcmd.xt -dsrmx=$dsrmx
fi

# Plot ray paths and travel times.
if [ $hasArgumentPlotryxt -eq 1 ]; then
  $bashPath/plotryxt.bash -fry=$fcmd-dsr$dsrmx.ry -fxt=$fcmd-dsr$dsrmx.xt
fi

If you wish to shorten your code a bit you can use test

test $hasArgumentRytr -gt 1 && $bashPath/plotryxt.bash -fry=$fcmd.ry -fxt=$fcmd.xt

Since you have same test on $hasArgumentRytr you can use just one if statement for this, not two.

Also, if you paste your entire code, it will be easier to help with shortening.

Hope that helps.

Regards
Peasant.

It is possible, but makes the code difficult to read and therefore difficult to debug. As a rule of thumb place a semi-colon where a line-feed would have been mandatory.

a=1 ; b=2 ; if [ $a -eq $b ] ; then echo "equal" ; else echo "not equal" ; fi

I much prefer the indented code:

a=1
b=2
if [ $a -eq $b ]
then
      echo "equal"
else
      echo "not equal"
fi

That is synonymous with this:

[ $hasArgumentRytr -eq 1 ] && $bashPath/plotryxt.bash -fry=$fcmd.ry -fxt=$fcmd.xt

It is shorter, but I don't think that does much to improve readability...

@OP: Are these arguments mutually exclusive? Then you could use a single variable that can have multiple values and use a case statement...

#!/bin/bash

# --Initialization----------------------------------------------------------------------------------

# Recognize both the -n flag and backslashed escape sequences.
echo_style="both"

# Set defaults
Def_raytrac_vrbLevel="medium"

hasArgumentCModInfile=0
hasArgumentSrcsInfile=0
hasArgumentRcvsInfile=0
hasArgumentRaysOutfile=0
hasArgumentTrvTOutfile=0

ierr=0
hasArgumentRaytracPath=0
hasArgumentPhases=0
hasArgumentLevel=0
hasArgumentFormat=0
hasArgumentDtau=0
hasArgumentBracD=0
hasArgumentTwPtD=0
hasArgumentTwPtItmax=0
hasArgumentRays=0
hasArgumentTrvT=0
hasArgumentPf=0
hasArgumentRaytracBg=0
hasArgumentRaytracVrbLevel=0

hasArgumentVerbose=0
hasArgumentQuiet=0
hasArgumentUsage=0
hasArgumentExamples=0
hasArgumentHelp=0

hasArgumentBDirPath=0
hasArgumentBDirColPos=0
hasArgumentBDirSortF=0
hasArgumentBDirGroupT=0
hasArgumentBDirAllFiles=0
hasArgumentBDirRaytrac=0
hasArgumentBDirDrw=0
hasArgumentBDirSmp=0
hasArgumentBDirLog=0
hasArgumentBDirMsf=0
hasArgumentBDirVrbLevel=0
hasArgumentBDirQuiet=0
hasArgumentBDirVerbose=1             # Default verbosity for script browseDir.tcsh
hasArgumentBDirFileLst=0

Def_vrbLevel=1
Def_shiftLen=3
Def_browseDir_vrbLevel=1

vrb_usrInputFlag=0
arg_browseDir_fileLst=""

# --Read Command Line Arguments---------------------------------------------------------------------

# IFS is a special variable that controls splitting. We split on "|" and "=", not whitespace.
# "$*" will now become a string like a|b|c

# Split on | and =.  $* will insert the | itself when IFS="|", so we
# get back what we put in, except everything's now split on = too, i.e.
# "a b" "c" --option=file
# becomes
# "a b" "c" --option file


# Return IFS to its normal whitespace.

# Loop until all arguments are used up.
# Shift tosses the value of $1 and sets $1=$2, $2=$3, ...
# so doing this repeatedly will decrease the value of $# until
# we run out of arguments.

echo "1. opt_cmodInfile = $opt_cmodInfile"

OLDIFS="$IFS"
IFS="|="                # IFS controls splitting. Split on "|" and "=", not whitespace.
set -- $*               # Set the positional parameters to the command line arguments.
IFS="$OLDIFS"

narg="$#"
opt_browseDir_flag=`echo "$*" | tr '' '' | awk '/BRWS/ {print 1}; ! /BRWS/ {print 0}'`
echo "\$* = $*"

while [ "$#" -gt 0 ]
do

  case "$1" in

  "--"[rR][aA][yY][tT][rR][aA][cC]"-"[pP][aA][tT][hH])
    shift               # Skip ahead one to the next argument.
    arg_progPath="${1}"
    opt_progPath=1
  ;;

  "--"[cC][mM][oO][dD][iI][fF]|\
  "--"[cC][mM][oO][dD]"-"[iI][nN][fF][iI][lL][eE])
    shift
    arg_cmodInfile="${1}"
    hasArgumentCModInfile=1
    echo "cmodif option chosen: opt_cmodInfile = $opt_cmodInfile"
  ;;

  "--"[sS][rR][cC][sS][iI][fF]|\
  "--"[sS][rR][cC][sS]"-"[iI][nN][fF][iI][lL][eE]|\
  "--"[sS][oO][uU][rR][cC][eE][sS]"-"[iI][nN][fF][iI][lL][eE])
    shift
    arg_srcsInfile="${1}"
    hasArgumentSrcsInfile=1
  ;;

  "--"[rR][cC][vV][sS][iI][fF]|\
  "--"[rR][cC][vV][sS]"-"[iI][nN][fF][iI][lL][eE]|\
  "--"[rR][eE][cC][eE][iI][vV][eE][rR][sS]"-"[iI][nN][fF][iI][lL][eE])
    shift
    arg_rcvsInfile="${1}"
    hasArgumentRcvsInfile=1
  ;;

  "--"[pP][hH][sS][sS]|\
  "--"[pP][hH][aA][sS][eE][sS])
    shift
    arg_phases="${1}"
    hasArgumentPhases=1
  ;;

  "--"[eL][vV]|\
  "--"[lL][eE][vV][eE][lL])
    shift
    arg_level="${1}"
    hasArgumentLevel=1
  ;;

  "--"[fF][mM][tT]|\
  "--"[fF][oO][rR][mM][aA][tT])
    shift
    arg_format="${1}"
    hasArgumentFormat=1
  ;;

  "--"[dD][tT][aA][uU])
    shift
    arg_dtau="${1}"
    hasArgumentDtau=1
  ;;

  "--"[bB][rR][aA][cC][dD]|\
  "--"[bB][rR][aA][cC]"-"[dD][iI][sS][tT])
    shift
    arg_bracDist="${1}"
    hasArgumentBracD=1
  ;;

  "--"[tT][wW][pP][tT][dD]|\
  "--"[tT][wW][pP][tT]"-"[dD][iI][sS][tT])
    shift
    arg_twptDist="${1}"
    hasArgumentTwPtD=1
  ;;

  "--"[iI][tT][mM][aA][xX]-[tT][wW][pP][tT]|\
  "--"[mM][aA][xX][iI][tT][eE][rR][tT][pP])
    shift
    arg_maxitertp="${1}"
    hasArgumentTwPtItmax=1
  ;;

  "--"[rR][aA][yY][sS][oO][fF]|\
  "--"[rR][aA][yY][sS]"-"[oO][uU][tT][fF][iI][lL][eE])
    shift
    arg_raysOutfile="${1}"
    hasArgumentRaysOutfile=1
  ;;

  "--"[tT][rR][vV][tT][oO][fF]|\
  "--"[tT][rR][vV][tT]"-"[oO][uU][tT][fF][iI][lL][eE])
    shift
    arg_trvtOutfile="${1}"
    hasArgumentTrvTOutfile=1
  ;;

  "--"[rR][aA][yY][tT][rR][aA][cC]"-"[vV]|\
  "--"[rR][aA][yY][tT][rR][aA][cC]"-"[vV][rR][bB]"-"[lL][eE][vV][eE][lL])
    shift
    arg_vrbRaytracLevel="${1}"
    hasArgumentRaytracVrb=1
  ;;

  "--"[pP][fF]|\
  "--"[pP][fF][iI][lL][eE])
    shift
    arg_pfile="${1}"
    hasArgumentPFile=1
  ;;

  "--"[rR][aA][yY][tT][rR][aA][cC]"-"[bB][gG])
    hasArgumentRaytracBg=1
  ;;

  "-"[vV]|\
  "--"[vV][rR][bB]"-"[lL][eE][vV][eE][lL])
    shift
    arg_vrbLevel="${1}"
    hasArgumentVrb_usrInputFlag=$usrInputFlag
    hasArgumentVerbose=1
  ;;

  "-"[qQ]|\
  "--"[qQ][uU][iI][eE][tT])
    hasArgumentVerbose=0
  ;;

  "-"[uU]|\
  "--"[uU][sS][aA][gG][eE])
    hasArgumentUsage=1
  ;;

  "-"[eE]|\
  "--"[eE][xX][aA][mM][pP][lL][eE][sS])
    hasArgumentExamples=1
  ;;

  "-"[hH]|\
  "--"[hH][eE][lL][pP])
    hasArgumentHelp=1
  ;;

  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[pP][aA][tT][hH])
    arg_browseDir_Path=$par
    hasArgumentBDirPath=1
  ;;

  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[cC][hH][kK]|\
  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[cC][hH][eE][cC][kK]|\
  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[cC][hH][eE][cC][kK]"-"[cC][oO][lL][pP][oO][sS])
    hasArgumentBDirColPos=1
  ;;

  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[O][R][T]|\
  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[O][R][T]"-"[F][E][L][D])
    shift
    arg_browseDir_sortPtn="${1}"
    hasArgumentBDirSort=1
  ;;

  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[gG][rR][oO][uU][pP]|\
  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[gG][rR][oO][uU][pP]"-"[tT][aA][bB][lL][eE])
    shift
    arg_browseDir_groupPtn="${1}"
    hasArgumentBDirGroup=1
  ;;

  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[A][L][L]|\
  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[A][L][L]"-"[F][L][E])
    hasArgumentBDirAll=1
  ;;

  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[rR][aA][yY][tT][rR][aA][cC]|\
  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[rR][aA][yY][tT][rR][aA][cC]"-"[fF][iI][lL][eE][sS])
    hasArgumentBDirRaytrac=1
  ;;

  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[dD][rR][wW]|\
  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[dD][rR][wW]"-"[fF][iI][lL][eE][sS])
    hasArgumentBDirDrw=1
  ;;

  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[sS][mM][pP]|\
  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[sS][mM][pP]"-"[fF][iI][lL][eE][sS])
    hasArgumentBDirSmp=1
  ;;

  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[lL][oO][gG]|\
  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[lL][oO][gG]"-"[fF][iI][lL][eE][sS])
    hasArgumentBDirLog=1
  ;;

  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[mM][sS][fF]|\
  "--"[bB][rR][wW][sS][dD][iI][rR]"-"[mM][sS][fF]"-"[fF][iI][lL][eE][sS])
    hasArgumentBDirMsf=1
  ;;

  *)
    arg_browseDir_fileLst="$arg_browseDir_fileLst ${1}"
    hasArgumentBDirFileLst=1
  ;;

  esac

  shift                 # Skip ahead to the next argument

done

echo "2. opt_cmodInfile = $opt_cmodInfile"

echo "arg_browseDir_fileLst = $arg_browseDir_fileLst"

# --Print Header------------------------------------------------------------------------------------

allArgs="$*"

raytracProg="./raytrac.tcsh"
eg1="$raytracProg --cmodif"
eg2="$raytracProg --cmodif"
eg3="$raytracProg --cmodif"

echo "opt_usage = $opt_usage"
echo "opt_examples = $opt_examples"
echo "opt_help = $opt_help"
echo "narg = $narg"

if [ $hasArgumentUsage -eq 1 ]; then
  echo ""
  echo -n "\033[2;32m"
  echo "-- Usage ----------------------------------------------------------------------------------"
  echo -n "\033[0m"
  echo ""
  echo "  \033[2;34m./raytrac.tcsh Args\033[0m"
  echo ""
  echo -n "\033[2;32m"
  echo "-- Mandatory Arguments --------------------------------------------------------------------"
  echo -n "\033[0m"
  echo ""
  echo "  \033[2;32m"
  echo "-- Optional Arguments ---------------------------------------------------------------------"
  echo -n "\033[0m"
  echo ""
  echo "   --raytrac-path=dir"
  echo "   --cmdif,             --cmd-infile=file"
  echo "   --srcsif,            --srcs-infile         --sources-infile=file"
  echo "   --rcvsif,            --rcvs-infile,        --receivers-infile=file"
  echo "   --pmff               --pmfl-outfile=file"
  echo "   --phss,              --phases=phases"
  echo "   --lv,                --level=level"
  echo "   --fmt,               --format=format"
  echo "   --dtau=time"
  echo "   --bracD,             --bracDist=dist"
  echo "   --twptD,             --twptDist=dist"
  echo "   --itmxtp,            --maxitertp=int"
  echo "   --raytrac-v, --raytrac-verbose, --raytrac-vrb-level, --raytrac-verbosity-level=level"
  echo "   --raytrac-bg"
  echo "   --fry,               --rays-file=file"
  echo "   --fxt,               --trvlt-file=file"
  echo "   --flog,              --log-file=file"
  echo "   -v, --verbose, --vrb-level, --verbosity-level=level"
  echo "   -q,                  --quiet"
  echo "   -u,                  --usage"
  echo "   -e,                  --examples"
  echo "   -h,                  --help"
  echo ""
  echo "   --BDir-path=dir"
  echo "   --BDir-check,     --brwsdir-check-colpos"
  echo "   --BDir-sort,      --brwsdir-sort-fields"
  echo "   --BDir-group,     --brwsdir-group-table"
  echo "   --BDir-all,       --brwsdir-all-files"
  echo "   --BDir-raytrac,   --brwsdir-raytrac-files"
  echo "   --BDir-drw,       --brwsdir-drw-files"
  echo "   --BDir-smp,       --brwsdir-smp-files"
  echo "   --BDir-log,       --brwsdir-log-files"
  echo "   --BDir-msf,       --brwsdir-msf-files"
  echo ""
  echo -n "\033[2;32m"
  echo "-------------------------------------------------------------------------------------------"
  echo -n "\033[0m"
  echo "\033[2;42;1;37m \033[0m"
  echo ""
  exit 0
fi

if [ $hasArgumentExamples -eq 1 ]; then
  echo ""
  echo -n "\033[2;32m"
  echo "-- Examples -------------------------------------------------------------------------------"
  echo -n "\033[0m"
  echo ""
  echo "  $eg1"
  echo "  $eg2"
  echo "  $eg3"
  echo ""
  echo -n "\033[2;32m"
  echo "-------------------------------------------------------------------------------------------"
  echo -n "\033[0m"
  echo "\033[2;42;1;37m \033[0m"
  echo ""
  exit 0
fi

if [ $hasArgumentHelp -eq 1 ] || [ $narg -eq 0 ]; then
  echo ""
  echo -n "\033[2;32m"
  echo "-- Name -----------------------------------------------------------------------------------"
  echo -n "\033[0m"
  echo ""
  echo "  \033[2;34mraytrac.tcsh\033[0m"
  echo "    Script to run raytrac program."
  echo ""
  echo -n "\033[2;32m"
  echo "-- Usage ----------------------------------------------------------------------------------"
  echo -n "\033[0m"
  echo ""
  echo "  \033[2;34m./raytrac.tcsh Args [files]\033[0m"
  echo ""
  echo -n "\033[2;32m"
  echo "-- Optional Arguments -------------------------------------------------------------------"
  echo -n "\033[0m"
  echo ""
  echo "   \033[2;34m--raytrac-path=dir\033[0m"
  echo "     The directory path of raytrac program."
  echo ""
  echo "   \033[2;34m-cmdif, --cmd-infile=file\033[0m"
  echo "     File containing starting sound speed model."
  echo ""
  echo "   \033[2;34m-srcsif, --srcs-infile, sources-infile=file\033[0m"
  echo "     File containing source locations."
  echo ""
  echo "   \033[2;34m-rcvsif, --rcvs-infile, --receivers-infile=file\033[0m"
  echo "     File containing receiver locations."
  echo ""
  echo "   \033[2;34m-ph, --phases=str\033[0m"
  echo "     Seismic phases to model (separated by '/')."
  echo ""
  echo "       RT    Root of the map"
  echo "       SP    Shot P"
  echo "       SS    Shot S"
  echo "       RP    Reflected as P"
  echo "       RS    Reflected as S"
  echo "       TS    Transmitted as S"
  echo "       TP    Transmitted as P"
  echo "       FS    Free surface"
  echo ""
  echo "       Default is 'P' which is equivalent to 'SP/FS'."
  echo ""
  echo "   \033[2;34m-lv, --level=level\033[0m"
  echo "     Raytracing level (shoot/brack/twop)."
  echo ""
  echo "   \033[2;34m-fmt, --format=format\033[0m"
  echo "     Output format, default is 'X/T'"
  echo ""
  echo "   \033[2;34m-dtau=time\033[0m"
  echo "     Ray integration step."
  echo ""
  echo "   \033[2;34m-bracD, --bracDist=dist\033[0m"
  echo "     Maximum ray to receiver distance for a bracketing ray."
  echo ""
  echo "   \033[2;34m-twptD, --twptDist=dist\033[0m"
  echo "     Maximum ray to receiver distance for two-point ray tracing."
  echo ""
  echo "   \033[2;34m-imxtp, --maxitertp=int\033[0m"
  echo "     Maximum number of iterations for two-point ray tracing."
  echo ""
  echo "   \033[2;34m-vrt, --vrb-raytrac=level\033[0m"
  echo "     Set verbosity level ro raytrac."
  echo ""
  echo "   \033[2;34m-bgrt, --bg-raytrac\033[0m"
  echo "     Runs raytrac in background."
  echo ""
  echo "   \033[2;34m-pf, --pfile=pfile.pf\033[0m"
  echo "     Set name of parameter file."
  echo ""
  echo "   \033[2;34m-fry, -frays=file\033[0m"
  echo "     Set name of ray paths (.ry) file."
  echo ""
  echo "   \033[2;34m-fxt, --ftrvt=file\033[0m"
  echo "     Set name of travel times (.xt) file."
  echo ""
  echo "   \033[2;34m-flog=file.log\033[0m"
  echo "     Set name of raytrac log file."
  echo ""
  echo "   \033[2;34m-v, --vrb-level=level\033[0m"
  echo "     Set the verbosity level (0,1,2,3). Level 0 is almost no logging, 3 is log everything."
  echo "     Level 1, the default, is a safe middle road."
  echo ""
  echo "   \033[2;34m-q, --quiet\033[0m"
  echo "     No output is printed."
  echo ""
  echo "   \033[2;34m-u,       --usage\033[0m"
  echo "     Print usage information on script browseDir and exits."
  echo ""
  echo "   \033[2;34m-e,       --examples\033[0m"
  echo "     Print some examples on using script browseDir and exits."
  echo ""
  echo "   \033[2;34m-h,       --help\033[0m"
  echo "     Print help information on usinf script browseDir and exits."
  echo ""
  echo "   \033[2;34m--brwsdir-path=dir\033[0m"
  echo "     Set the filesystem path for listing files."
  echo ""
  echo "   \033[2;34m--brwsdir-check,     --brwsdir-check-colpos\033[0m"
  echo "     Check column position."
  echo ""
  echo "   \033[2;34m--brwsdir-sort,      --brwsdir-sort-fields\033[0m"
  echo "     Sort files according to specific fields in the file name."
  echo ""
  echo "   \033[2;34m--brwsdir-group,     --brwsdir-group-table\033[0m"
  echo "     Group files according to specific fields in the file name."
  echo ""
  echo "   \033[2;34m--brwsdir-all,       --brwsdir-all-files\033[0m"
  echo "     List files in specified filesystem path."
  echo ""
  echo "   \033[2;34m--brwsdir-raytrac,   --brwsdir-raytrac-files\033[0m"
  echo "     List files related to program raytrac."
  echo ""
  echo "   \033[2;34m--brwsdir-drw,       --brwsdir-drw-files\033[0m"
  echo "     List files related to program tdarwin."
  echo ""
  echo "   \033[2;34m--brwsdir-smp,       --brwsdir-smp-files\033[0m"
  echo "     List files related to program tsimplex."
  echo ""
  echo "   \033[2;34m--brwsdir-log,       --brwsdir-log-files\033[0m"
  echo "     List log files."
  echo ""
  echo "   \033[2;34m--brwsdir-msf,       --brwsdir-msf-files\033[0m"
  echo "     List misfit files"
  echo ""
  echo "   \033[2;34m--brwsdir-v,         --brwsdir-vrb-level\033[0m"
  echo "     Set the verbosity level (0,1,2,3) of script browseDir. Level 0 is almost no logging,"
  echo "     level 3 is log everything. Level 1, the default, is a safe middle road."
  echo ""
  echo "   \033[2;34m--brwsdir-q,         --brwsdir-quiet\033[0m"
  echo "     No output is printed by script browseDir."
  echo ""
  echo -n "\033[2;32m"
  echo "-- Examples -------------------------------------------------------------------------------"
  echo -n "\033[0m"
  echo ""
  echo "  $eg1"
  echo ""
  echo "  $eg2"
  echo ""
  echo "  $eg3"
  echo ""
  echo -n "\033[2;32m"
  echo "-------------------------------------------------------------------------------------------"
  echo -n "\033[0m"
  echo "\033[2;42;1;37m \033[0m"
  echo ""
  exit 0
fi

# Set directory path to raytrac program.

programFullName=`basename $0`
echo "programFullName = $programFullName"

bashPath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "bashPath = $bashPath"
progPath=`echo "$bashPath" | awk 'BEGIN {FS="/bash"} {print $1"/prog"}'`
echo "progPath = $progPath"

#set DefRaytracPath = `echo $0:h | awk 'BEGIN {FS="/tcsh"} {print $1"/prog"}'`
#DefRaytracPath=`echo `echo $0 | sed -e 's|/[^/]*$||'` | awk 'BEGIN {FS="/tcsh"} {print $1"/prog"}'`

# Check for errors

#set errLst = ""
#foreach f ($arg_browseFilesLst)
#  set cerr = `echo "$f" | grep "[ ; : ' , ? { } $ = ]"`
#  set cerr = `echo $f | awk '/^-|=/'`
#  if (${%cerr} > 0) then                            # Matches '-' at beginning or '='
#    set errLst = "$errLst $f"
#    set ierr = 1
#  endif
#end

#[:alnum:]
#[:punct:]

#if (${%errLst} > 0) then
#  echo "\033[5;41;1;37m   *** ERROR ***   \033[0m"
#  echo "   The following file names are not valid:\n"
#  $raytracPath/printTable.tcsh --shift=3 -q $errLst
#  echo "\033[5;41;1;37m \033[0m"
#  echo ""
#  exit 1
#endif

if [ $hasArgumentQuiet -eq 1 ]; then
  opt_verbose=0
  vrbLevel=0
elif [ $vrb_usrInputFlag -eq 1 ]; then
  vrbLevel="$arg_vrbLevel"
else
  vrbLevel="$Def_vrbLevel"
fi

if [ $hasArgumentBDirFileLst -eq 1 ]; then
  browseDir_fileLst="$arg_browseDir_fileLst"
  if [ $opt_browseDir_flag -eq 0 ]; then
    opt_browseDir_flag=1
  fi
fi

# -- User requested running browseDir --------------------------------------------------------------

# -- Set the command-line arguments for browseDir.tcsh -------------------------------------------

echo "opt_browseDir_flag = $opt_browseDir_flag"
if [ $opt_browseDir_flag -eq 1 ]; then

  browseDir_clArgs=""
  if [ $hasArgumentBDirFileLst -eq 0 ]; then
    if [ $hasArgumentBDirColPos -eq 1 ]; then
      browseDir_clArgs="--check"
    elif [ $hasArgumentBDirRaytrac -gt 0 ]; then
      browseDir_clArgs="--raytrac"
    elif [ $hasArgumentBDirDrw -gt 0 ]; then
      browseDir_clArgs="--drw"
    elif [ $hasArgumentBDirSmp -gt 0 ]; then
      browseDir_clArgs="--smp"
    elif [ $hasArgumentBDirLog -gt 0 ]; then
      browseDir_clArgs="--log"
    elif [ $hasArgumentBDirMsf -gt 0 ]; then
      browseDir_clArgs="--msf"
    fi
  fi

  if [ $hasArgumentBDirSort -eq 1 ]; then
    browseDir_clArgs="$browseDir_clArgs --sort"
  fi

  if [ $hasArgumentBDirGroup -eq 1 ]; then
    browseDir_clArgs="$browseDir_clArgs --group"
  fi

  if [ $hasArgumentBDirVerbose -eq 1 ]; then
    if [ $hasArgumentBDirVrbLevel -eq 1 ]; then
      browseDir_clArgs="$browseDir_clArgs --vrb-level"
    fi
  elif [ $hasArgumentBDirQuiet -eq 1 ]; then
    browseDir_clArgs="$browseDir_clArgs --quiet"
  fi

  # -- Run browseDir.tcsh --------------------------------------------------------------------------

  echo "browseDir_clArgs = $browseDir_clArgs"
  echo "RUN BROWSEDIR"

  if [ $vrbLevel -gt 1 ]; then
    echo "\n   $raytracPath/browseDir.tcsh $browseDir_clArgs"
  fi

  echo "bashPath = $bashPath"

  if [ $hasArgumentBDirFileLst -eq 1 ]; then
    echo "browseDir.tcsh browseDir_clArgs browseDir_fileLst"
    $bashPath/browseDir.bash $browseDir_clArgs $browseDir_fileLst
  else
    echo "browseDir.tcsh browseDir_clArgs"
    $bashPath/browseDir.bash $browseDir_clArgs
  fi

  echo ""
  exit 0

fi

#set arg_browseDirPth = $par
#set opt_browseDirPth = 1

#if ($opt_browseDirPth == 1) then
#  set vPth = `echo $arg_browsePth | sed 's/[\/]*$//g'`   # Removes any number of "/" at the end
#else if ($opt_browseDirPth == 0) then
#  set browseDirPth = `pwd`
#endif

# Search sound speed model file in current directory.

cmodInfileLst=""

if [ $hasArgumentCModInfile -eq 0 ]; then

  cmodInfileLst=`ls -lrt *.cmod | awk 'BEGIN {ORS=" "}; {print $NF}'`
  NF=`echo $cmodInfileLst | awk '{print NF}'`

  if [ $NF -eq 0 ]; then
    echo ""
    echo "\033[5;41;1;37m   *** ERROR ***   \033[0m"
    echo "There are no .cmod files"
    echo "\033[5;41;1;37m \033[0m"
    echo ""
    exit 1
  elif [ $NF -gt 1 ]; then
    echo "  There are $NF .cmod files:\n"
    i=1
    cmodInfileArr="$cmodInfileLst"
    for f in $cmodInfileLst ; do
      echo "  File $i = $f"
      (($i++))
    done
    echo "  Set the starting model file using --cmodif"
    echo ""
    echo "-----------------------------------------------------------------------------------------"
    echo ""
    exit 1
  fi

fi

# Search sources files in current directory.

srcsInfileLst=""

if [ $hasArgumentSrcsInfile -eq 0 ]; then

  srcsInfileLst=`ls -lrt *.srcs | awk 'BEGIN {ORS=" "}; {print $NF}'`
  NF=`echo $srcsInfileLst | awk '{print NF}'`

  if [ $NF -eq 0 ]; then
    echo "\033[5;41;1;37m   *** ERROR ***   \033[0m"
    echo "  No sources files found."
    echo "\033[5;41;1;37m \033[0m"
    echo ""
    exit 1
  elif [ $NF -gt 1 ]; then
    echo ""
    echo "\033[5;41;1;37m   *** ERROR ***   \033[0m"
    echo "  $NF sources files found."
    i=1
    srcsInfileArr="$srcsInfileLst"
    for f in $srcsInfileLst ; do
      echo "  File $i = $f"
      (($i++))
    done
    echo "Set the sources file using --sources."
    echo "\033[5;41;1;37m \033[0m"
    echo ""
    exit 1
  fi

fi

# Search receivers files in current directory.

rcvsInfileLst=""

if [ $hasArgumentRcvsInfile -eq 0 ]; then

  rcvsInfileLst=`ls -lrt *.rcvs | awk 'BEGIN {ORS=" "}; {print $NF}'`
  NF=`echo $rcvsInfileLst | awk '{print NF}'`

  if [ $NF -eq 0 ]; then
    echo ""
    echo "\033[5;41;1;37m   *** ERROR ***   \033[0m"
    echo "No receivers files found."
    echo "\033[5;41;1;37m \033[0m"
    echo ""
    exit 1
  elif [ $NF -gt 1 ]; then
    echo ""
    echo "\033[5;41;1;37m   *** ERROR ***   \033[0m"
    echo "  $NF receivers files found."
    i=1
    rcvsInfileArr="$rcvsInfileLst"
    for f in $rcvsInfileLst ; do
      echo "  File $i = $f"
      (($i++))
    done
    echo "  Set the receivers file using --receivers."
    echo "\033[5;41;1;37m \033[0m"
    echo ""
    exit 1
  fi

fi

# --Parameterization--------------------------------------------------------------------------------

# SET DEFAULT ARGUMENTS

# Set directory path to Tommy tsimplex in tommy/bin/prog.
if [ $hasArgumentRaytracPath -eq 1 ]; then
  raytracPath=$arg_raytracPath
else
  raytracPath="$DefRaytracPath"
fi

echo "opt_cmodInfile = $opt_cmodInfile"
# Search model files in current directory
if [ $hasArgumentCModInfile -eq 0 ]; then
  arg_cmodInfile=`ls -lrt *.cmod | awk 'BEGIN {ORS=" "}; {print $NF}'`
fi

# Search sources files in current directory
if [ $hasArgumentSrcsInfile -eq 0 ]; then
  arg_srcsInfile=`ls -lrt *.srcs | awk 'BEGIN{ORS=" "}; {print $NF}'`
fi

# Search receiver files in current directory
if [ $hasArgumentRcvsInfile -eq 0 ]; then
  arg_rcvsInfile=`ls -lrt *.rcvs | awk 'BEGIN {ORS=" "}; {print $NF}'`
fi

if [ $hasArgumentPhases -eq 0 ]; then
  arg_phases="P"
fi

if [ $hasArgumentLevel -eq 0 ]; then
  arg_level="twopt"
fi

if [ $hasArgumentFormat -eq 0 ]; then
  arg_format="X T"
fi

if [ $hasArgumentDtau -eq 0 ]; then
  arg_dtau=0.1
fi

if [ $hasArgumentBracD -eq 0 ]; then
  arg_bracDist=0.3
fi

if [ $hasArgumentTwPtD -eq 0 ]; then
  arg_twptDist=0.03
fi

if [ $hasArgumentTwPtItmax -eq 0 ]; then
  arg_maxitertp=25
fi

if [ $hasArgumentRaysOutfile -eq 0 ]; then
  arg_raysOutfile=`echo $arg_cmodInfile | awk 'BEGIN {FS="."} {print $1".ry"}'`
fi

if [ $hasArgumentTrvTOutfile -eq 0 ]; then
  arg_trvtOutfile=`echo $arg_cmodInfile | awk 'BEGIN {FS="."} {print $1".xt"}'`
fi

if [ $hasArgumentPFile -eq 0 ]; then
  arg_pmflOutfile=`echo $arg_cmodInfile | awk 'BEGIN {FS="."} {print $1".pmfl"}'`
fi

# SET PARAMETERS

cmodInfile=$arg_cmodInfile
srcsInfile=$arg_srcsInfile
rcvsInfile=$arg_rcvsInfile

# Replace every '/' with a space " ".
phases=`echo $arg_phases | awk '{gsub(/\//," ")}; {print}'`
format=`echo $arg_format | awk '{gsub(/\//," ")}; {print}'`

level=$arg_level
dtau=$arg_dtau
bracDist=$arg_bracDist
twptDist=$arg_twptDist
maxitertp=$arg_maxitertp
pfile=$arg_pmflOutfile

if [ $hasArgumentRTracVrbLevel -eq 0 ]; then
  raytracVrbLevel="$Def_raytracVrbLevel"
else
  rtracVrbLevel="$arg_rtracVrbLevel"
fi
echo "rtracVrbLevel = $rtracVrbLevel"

fry=$arg_fry
fxt=$arg_fxt
flog=`echo $arg_fcmd | awk 'BEGIN {FS="."} {print $1"-raytrac.log"}'`

# --Print Header------------------------------------------------------------------------------------

if [ $hasArgumentVerbose -eq 1 ]; then
  echo ""
  echo -n "\033[2;34m"
  echo "-- Program --------------------------------------------------------------------------------"
  echo -n "\033[0m"
  echo ""
  echo "   raytrac.tcsh"
  echo "     Run Tommy raytrac"
  echo ""
  echo "-------------------------------------------------------------------------------------------"
  echo ""
  echo "  Files containing the location of the sources and receivers"
  echo "    $arg_srcsInfile"
  echo "    $arg_rcvsInfile"
  echo ""
  echo "  Output files containing the ray paths and the travel times"
  echo "    $arg_raysOutfile"
  echo "    $arg_xt"
  echo ""
  echo "  Sound speed model"
  echo "    $arg_cmdInfile"
  echo ""
  echo "  Directory name for running raytrac"
  echo "    $arg_raytracPath"
  echo ""
  echo "  Ray tracing parameters"
  echo "    Phases    = $arg_phases"                                 # see map.h
  echo "    Level     = $arg_level"
  echo "    dtau      = $arg_dtau"
  echo "    bracDist  = $arg_bracDist"
  echo "    twptDist  = $arg_twptDist"
  echo "    maxitertp = $arg_maxitertp"
  echo ""
  echo "  Output format for raytrac"
  echo "    $arg_format"
  echo ""
  echo "  Verbosity level for raytrac"
  echo "    $arg_vrbRaytracLevel"
  echo ""
  echo "  Parameter file"
  echo "    $pfile"

  if [[$hasArgumentRTracBg -eq 1 ]]; then
    echo "  --bg-raytrac"
  fi

  echo ""
fi

# --Create parameter file---------------------------------------------------------------------------

# $* passes all of the arguments to the script.

txt1="$raytracPath/raytrac --vmod
txt2="--phases
txt3="mdacc
txt4="--ray
txt5="$txt1 $txt2 $txt3 $txt4"

echo "" > $pfile
echo "Call to raytrac.tcsh" >> $pfile
echo "$raytracPath/raytrac.tcsh $*\n\n\n" >> $pfile

echo "Parameters used in Tommy raytrac run" >> $pfile
echo "-----------------------------------------------------------------------------------" >> $pfile

echo "Call to Tommy raytrac program" >> $pfile
echo "$txt1 $txt2 $txt3 $txt4 $txt5" >> $pfile

# --Run raytrac-------------------------------------------------------------------------------------

echo "--Run raytrac program------------------------------------------------------------------------"

# Log option. Writes logging information into a log file at the specified
# existing path. The path to the log file location must already exist. The
# installer does not create the directory structure for the logfile.

# The following information is entered into the log:

#    * Status messages
#    * Nonfatal warnings
#    * All error messages
#    * Start up of actions
#    * Action-specific records
#    * User requests
#    * Initial UI parameters
#    * Out-of-memory or fatal exit information
#    * Out-of-disk-space messages
#    * Terminal properties

echo "vmod=$cmodInfile"
echo "run $raytracPath/raytrac vmod=$cmodInfile srfile=$srcsInfile rcfile=$rcvsInfile"
raytracVrbLevel="medium"
echo "vrb=$raytracVrbLevel"

if [ $hasArgumentRTracBg -eq 0 ]; then
  $progPath/raytrac --vmod=$cmodInfile --srfile=$srcsInfile --rcfile=$rcvsInfile      \
    --phases="$phases" --level=$level --format="$format" --dtau=$dtau --mdacc=$bracDist  \
    --mindist=$twptDist --maxitertp=$maxitertp --ray=$raysOutfile --out=$trvtOutfile    \
    --vrb=$rtracVrbLevel 2>&1 | tee $logOutfile

elif [ $hasArgumentRTracBg -eq 1 ]; then
  $progPath/raytrac --vmod=$cmodInfile --srfile=$srcsInfile --rcfile=$rcvsInfile         \
    --phases="$phases" --level=$level --format="$format" --dtau=$dtau --mdacc=$bracDist  \
    --mindist=$twptDist --maxitertp=$twptItmax --ray=$raypOutfile out=$trvtOutfile       \
    --vrb=$rtracVrbLevel > $logOutfile 2>&1

fi

if [ $hasArgumentVrbLevel -eq 1 ]; then
  echo "\n\033[1;44;1;37m \033[0m\n"
elif [ $hasArgumentVrbLevel -gt 1 ]; then
  echo "\n\033[1;44;1;37m \033[0m \033[1;44;1;37m raytrac.tcsh \033[0m\n"
fi

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

I am also wondering if bash understand boolean variables for use in hasArgument, or whether it is best to keep the code as it is. I rather have readability than brevity.

---------- Post updated at 11:07 AM ---------- Previous update was at 10:50 AM ----------

I agree with you. No, arguments can be added as to user request. The user would be able to use a lot of settings.

bash understands strings. Integers are as close as it has to bool and even then, it's strings of decimal digits.

So I cannot have something similar to:

if [ $hasArgumentDist ]; then
  ...
fi

if [ ! $hasArgumentDist ]; then
  ...
fi

I like the shell built-ins true and false.

hasArgument1=true
hasArgument2=false

if $hasArgument1; then
  echo has argument 1
fi

if ! $hasArgument2; then
  echo does not have argument 2
fi

Sounds a good thing for my scripts when I am using hasArgument, rather than checking if it is 0 or 1 all the time. Correct me if I am wrong.

---------- Post updated at 01:56 PM ---------- Previous update was at 11:36 AM ----------

Currently I have a lot of options in the scripts, as shown below.

Thus I end up having hasArgument appended to every option. I am seriously thinking of having a function which I will call

which will be called as

Example:

returns true if the option Scrs was supplied by the user.

Is it possible to create such a function call and how would I use it. Seems a good idea at the moment, but would like some input on it.

I have also been thinking of using an associative array hasArgument

For example

hasArgument[Srcs]=true     # Option Srcs was set by the user.
hasArgumentCModInfile=0
hasArgumentSrcsInfile=0
hasArgumentRcvsInfile=0
hasArgumentRaysOutfile=0
hasArgumentTrvTOutfile=0

ierr=0
hasArgumentRaytracPath=0
hasArgumentPhases=0
hasArgumentLevel=0
hasArgumentFormat=0
hasArgumentDtau=0
hasArgumentBracD=0
hasArgumentTwPtD=0
hasArgumentTwPtItmax=0
hasArgumentRays=0
hasArgumentTrvT=0
hasArgumentPf=0
hasArgumentRaytracBg=0
hasArgumentRaytracVrbLevel=0

hasArgumentVerbose=0
hasArgumentQuiet=0
hasArgumentUsage=0
hasArgumentExamples=0
hasArgumentHelp=0

hasArgumentBDirPath=0
hasArgumentBDirColPos=0
hasArgumentBDirSortF=0
hasArgumentBDirGroupT=0
hasArgumentBDirAllFiles=0
hasArgumentBDirRaytrac=0
hasArgumentBDirDrw=0
hasArgumentBDirSmp=0
hasArgumentBDirLog=0
hasArgumentBDirMsf=0
hasArgumentBDirVrbLevel=0
hasArgumentBDirQuiet=0
hasArgumentBDirVerbose=1             # Default verbosity for script browseDir.tcsh
hasArgumentBDirFileLst=0

...

if [ $hasArgumentSrcsInfile -eq 1 ]; then

fi

if [ $hasArgumentRcvsInfile -eq 1 ]; then
  ...
fi

if [ $hasArgumentPhases -eq 1 ]; then
  ...
fi

if [ $hasArgumentLevel -eq 1 ]; then
  ...
fi

if [ $hasArgumentFormat -eq 1 ]; then
  ...
fi

if [ $hasArgumentDtau -eq 1 ]; then
  ...
fi

if [ $hasArgumentBracD -eq 1 ]; then
  ...
fi

if [ $hasArgumentTwPtD -eq 1 ]; then
  ...
fi

if [ $hasArgumentTwPtItmax -eq 1 ]; then
  ...
fi

if [ $hasArgumentRaysOutfile -eq 1 ]; then
  ...
fi

if [ $hasArgumentTrvTOutfile -eq 1 ]; then
  ...
fi

---------- Post updated at 02:40 PM ---------- Previous update was at 01:56 PM ----------