Conditions in if


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditions in if
# 1  
Old 08-03-2013
Conditions in if

I'm using the below one..

Code:
#!/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 Smilie
Code:
 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 ..

Code:
  elif [[ (${2} = -k) || (${2} = -f && ! $file ) ]] ;then

echo "Passed to multiple or file"
# 2  
Old 08-05-2013
I like to go 'case' if 'if' gets busy, but shell 'case' is a bit limited.
# 3  
Old 08-05-2013
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:
Code:
#!/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


Last edited by Don Cragun; 08-05-2013 at 09:58 PM.. Reason: Add missing <newline>...
# 4  
Old 08-06-2013
I share Don's suggest.

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

Code:
#!/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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Errors in if conditions.....

#if if then echo $varNO >> AgriN.csv fi done < data The above script throws error such as integer expression expected. How do i rectify that?? (4 Replies)
Discussion started by: nikhil jain
4 Replies

2. Shell Programming and Scripting

Errors in if conditions with to many OR conditions

Hi ALL I have a script where in i need to check for several values in if conditons but when i execute the script it throws error such as "TOO MANY ARGUMENTS" if then msg="BM VAR Issue :: bmaRequestVAR=$bmaRequestVAR , nltBMVAR=$nltBMVAR , bmaResponseVAR=$bmaResponseVAR ,... (10 Replies)
Discussion started by: nikhil jain
10 Replies

3. Shell Programming and Scripting

Zenity, While, and Conditions

All, I'm having fighting a losing battle with what I though would be simple. My goal is this: Show a zenity progress or info dialog until the system obtains an ip address, then close the dialog and continue through the rest of the script. Right now I've got the following: ip=`ifconfig |... (2 Replies)
Discussion started by: timbrammer91091
2 Replies

4. Shell Programming and Scripting

If conditions need

Dear Expert, Below code is for to take the backup of database by daily time stamp. I need vital help to make my script automatic sending me email if it sucess or fail. echo on @REM Seamonkey's quick date batch (MMDDYYYY format) @REM Setups %date variable @REM First parses month, day, and... (6 Replies)
Discussion started by: Alone
6 Replies

5. Shell Programming and Scripting

IF OR with two conditions

I have this IF working fine, testing if a char is a digit: if ; then _VALUE=$_VALUE$_CHAR else _ISDIGIT="false" fi Then I add a second condition to test if the char is either a digit or a * if ]; then _VALUE=$_VALUE$_CHAR ... (11 Replies)
Discussion started by: Flavius
11 Replies

6. Shell Programming and Scripting

While with three conditions

Currently this is what I am trying while || && ]; do I want to continue if the first condition or both the second and third are true but I am getting a too many arguments error. Can someone help me out? (5 Replies)
Discussion started by: whdr02
5 Replies

7. Shell Programming and Scripting

Help regarding multiple conditions

Hi All, I am new to shell scripting. Can any one say what is wrong in this if statement, that uses multiple conditions if then *************** else if ( -z $pcs && "$night_time_calc" > "$night_time" ) then ******************************** ... (4 Replies)
Discussion started by: ssenthilkumar
4 Replies

8. Shell Programming and Scripting

conditions

./script 89 The script will extract the last digit of the input parameter. example, that is 4. This will be compared to the last digit of the current day of the month ( like day 14; that is 4). A message will displayed on the screen indicating if the digits are the same or not. (1 Reply)
Discussion started by: singh is king
1 Replies

9. Shell Programming and Scripting

multiple if conditions

Guys, Im trying to have a script that evaluates multiple conditions : test.sh: if then echo "host $1" else if then echo "host $1" else echo $1 not valid exit 1 fi when I do ./test.sh brazil1 I get: (4 Replies)
Discussion started by: bashshadow1979
4 Replies

10. UNIX for Dummies Questions & Answers

2 or more if conditions

Hello, I have a file as follows: col no:1 2 3 4 5 6 7 8 9 10 11 a 4 226 226 ch:95024048-95027592, 1y224 of 3545 223 224 ident b 53 235 235 ch:148398-148401255, 1y184 of 3187 180 186 ident awk... (3 Replies)
Discussion started by: dr_sabz
3 Replies
Login or Register to Ask a Question