Conditions in if

I'm using the below one..

#!/bin/ksh

File=$3

if [ ${#} = 1 ] ; then
      echo "Script"
  
              elif [[ ${2} = -k -o (${2} = -f  && !  $File ) ]] ;then 
                echo "Passed k or f option"
        
                  else "Please check the Input passed"
fi

Command line argument is "k" or -f and file is exist then it will display "Passed k or f option" but getting error :frowning:

 elif [[ ${2} = -k -o (${2} = -f && ! = $File ) ]] ;then 

Command line: main.ksh -f file.txt

---------- Post updated at 04:33 PM ---------- Previous update was at 04:22 PM ----------

Got it working ..

  elif [[ (${2} = -k) || (${2} = -f && ! $file ) ]] ;then 
            echo "Passed to multiple or file"

I like to go 'case' if 'if' gets busy, but shell 'case' is a bit limited.

I strongly suggest using something like getopts when parsing command line arguments. When you use getopts to process options, you get the same capabilities all of the standard utilities use when parsing options: single-character options without option-arguments can be grouped behind a single minus sign or each can follow separate minus signs, options with option-arguments can be combined in a single operand or be presented as separate arguments, -- can be used to end option processing so following operands can start with a minus sign and not be mistaken for options, the order of options won't matter, etc.

From your example, it appeared that you code script could be invoked as:
./script , ./script -k , or ./script -f file . The following can be invoked with no options, with -k or -f file , or with both -k and -f file in any order. When the -f option is given, this script gives an error if the option argument is not a regular file. if you call this script with options other than -f and -k, it will tell you that it found an option it didn't expect. It also tells you how many operands are left after the options are processed and prints them.

Hope this helps:

#!/bin/ksh
IAm=${0##*/}
err=0
File=""
kflag=
while getopts f:k opt
do      case $opt in
        (f)     File="$OPTARG"
                if [ ! -f "$File" ]
                then    printf "Option -f \"%s\": Not a regular file\n" "$File" >&2
                        err=1
                fi;;
        (k)     kflag=1;;
        (?)     err=2;;
        esac
done
shift $(($OPTIND - 1))
if [ $err -gt 0 ]
then    printf "Usage: %s: [-k] [-f pathname] arg...\n" "$IAm" >&2
        exit $err
fi
if [ "$kflag" ]
then    printf "Option k found\n"
fi
if [ -n "$File" ]
then    printf "Option -f \"%s\" found; argument is a regular file\n" "$File"
fi
if [ $# -gt 0 ]
then    printf "%d remaining operands are:" $#
        printf " %s" "$*"
        echo
else    echo "No operands found."
fi

I share Don's suggest.

Here is my "standard" command line handler without ex. getopt.

#!/usr/bin/somesh  ex. ksh, bash, dash, ...
PRG=$0

#####################
usage()
{
     exitid=$1
     [ "$exitid" = "" ] && exitid=1

     echo " Usage: $PRG -f filename [ -d 0-9 ] [-v ] files" >&2
     echo " or  $PRG --filename filename [ --debug 0-9 ] [--verbose ] files" >&2
     exit $exitid
}

#####################
# set default 
filename=""
verbose=0
debug=0

# parse options: look 1st and shift it, if option need argument then double shift
while [ $# -gt 0 ]
do
        opt="$1"
        case "$opt" in
                -v|--verbose)  verbose=1              ;;
                -d|--debug)    debug="$2" ; shift    ;;
                -f|--filename) filename="$2" ; shift  ;;
                --)   shift; break ;; # options end
                -*)   usage 1 ;;  # unknown option
                *)    break ;;  # arguments ...
        esac
        shift # next option
done

# current status in args :  $* include arguments

# test parameters and if some problem: usage including exit
[ "$filename" = "" ] && usage 2
[ "$debug"    = "" ] && usage 3   # debug value can't be empty
[ "$#"       -lt 1   ] && usage 4   # no argument files

# OK, do it
#  use arguments ex. using for
for arg in $*
do
        echo "arg:$arg"
done