Sponsored Content
Full Discussion: if elseif fi
Top Forums Shell Programming and Scripting if elseif fi Post 302485409 by methyl on Wednesday 5th of January 2011 06:50:23 AM
Old 01-05-2011
There were a number of issues (missing "then", orphaned "elif" , wrong answer displayed etc.). In the end it is much easier to use a "case" statement for the options.
For rapid testing I have surrounded this example menu in a do-done loop and removed lots of "exit" statements. There are now no "else" or "elif" statements in the script which makes it much easier to follow.

Code:
while true
do

echo ""
echo "1.FILE PERMISSION"
echo "2.FILE INODE NUMBER"
echo "3.FILE SIZE"
echo "4.FILE OWNER"
echo " Enter what do you want from the option"
read num

case $num in
1)
    echo " Enter filename you want permissions for : "
    read file1
    if [ ! -f $file1 ]
    then
        echo " FILE NOT FOUND"
    fi
    echo " FILE PERMISSIONS for $file1 = `ls -ltr $file1 | awk '{print $1}'` "
    ;;
2)
    echo " Enter filename for which you want INODE NUMBER for : "
    read file1
    if [ ! -f $file1 ]
    then
        echo " FILE NOT FOUND"
    fi
    echo " FILE INODE NUMBER for $file1 = `ls -il $file1 | awk '{print $1}'` "
    ;;
3)
    echo " Enter filename for which you want SIZE  : "
    read file1
    if [ ! -f $file1 ]
    then
        echo " FILE NOT FOUND"
    fi
    echo " FILE SIZE for $file1 = `ls -lad $file1 | awk '{print $5}'` "
    ;;
4)
    echo " Enter filename for which you want OWNER for : "
    read file1
    if [ ! -f $file1 ]
    then
        echo " FILE NOT FOUND"
    fi
    echo " FILE OWNER for $file1 = `ls -lad $file1 | awk '{print $3}'` "
    ;;
*)
    echo "You have not chosen from the list"
    exit 0
    ;;
esac
#
done



@gcvinayak
Please start a new thread in future. Just realised that this is a completely different subject.

Last edited by methyl; 01-05-2011 at 07:57 AM..
This User Gave Thanks to methyl For This Post:
 

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk if elseif syntax error

Below is the code. nawk -F "|" 'FNR==NR{a=$3 OFS $4 OFS $5 OFS $6;next} {\ if ($5 in a)\ {print $1,"ABC",$5,"I",a, $2,$3,$4 OFS OFS OFS OFS OFS OFS OFS OFS $2"-"$3"-"$4} ; \ elseif ($5=="g")\ print $1,"ABC",$5,"I",$5 OFS OFS OFS OFS $2,$3,$4 OFS OFS OFS OFS OFS... (8 Replies)
Discussion started by: pinnacle
8 Replies

2. Shell Programming and Scripting

elseif in csh

I have been using the if statement in csh like this if ( $opt1 == 1 ) then ..... elseif ( $opt2 == 1 ) then ...... endif Seems to work, but got Badly placed ()'s. When I used a space in the elseif, a space between the 'else' and the 'if' it worked (0 Replies)
Discussion started by: kristinu
0 Replies

3. Linux

if elseif fi

Hi all, This is my first post in this forum, can i request you to guide, where i am going wrong with the error below. 34: Syntax error: "fi" unexpected (expecting "then") #!/bin/sh argCount=0 mysql_path=$USER_INSTALL_DIR$ for i in $*; do /A argCount+=1 done if ;then echo... (2 Replies)
Discussion started by: oracle_coorgi
2 Replies

4. UNIX for Dummies Questions & Answers

If Then ElseIf Script - Confusion Around Expression's Syntax

Hello, I am relatively new to UNIX scripting and am learning a lot. I have already tried several searches on this website and have tried various syntax options suggested to no avail. I am obviously not writing the script correctly. I really do appreciate any and all the help. Below is an... (8 Replies)
Discussion started by: dqrgk0
8 Replies

5. Programming

Problem with IF ELSEIF and GOTO statements in FORTRAN

Hi I am reading a book about Fortran 90 and I write the following code, to test my understanding of the first chapter. I have a problem with the last section of the code with deals with an IF, ELSEIF, and GOTO statements. Here is my Code PROGRAM sim ! This code is used to solve two... (3 Replies)
Discussion started by: faizlo
3 Replies

6. AIX

Elseif & for condition in AIX

I'm using the below statements in my script if && then sqlplus sysadm/abcdefgh12@${dbarr} @/u1/scripts/ResetPswd.sql elif then for idx in 0 1 2 3 4 5 6 7 do sqlplus sysadm/abcdefgh12@${dbarr} @/u1/scripts/ResetPswd.sql done else exit fi It give me... (5 Replies)
Discussion started by: Pandee
5 Replies
if(n)							       Tcl Built-In Commands							     if(n)

__________________________________________________________________________________________________________________________________________________

NAME
if - Execute scripts conditionally SYNOPSIS
if expr1 ?then? body1 elseif expr2 ?then? body2 elseif ... ?else? ?bodyN? _________________________________________________________________ DESCRIPTION
The if command evaluates expr1 as an expression (in the same way that expr evaluates its argument). The value of the expression must be a boolean (a numeric value, where 0 is false and anything is true, or a string value such as true or yes for true and false or no for false); if it is true then body1 is executed by passing it to the Tcl interpreter. Otherwise expr2 is evaluated as an expression and if it is true then body2 is executed, and so on. If none of the expressions evaluates to true then bodyN is executed. The then and else arguments are optional "noise words" to make the command easier to read. There may be any number of elseif clauses, including zero. BodyN may also be omitted as long as else is omitted too. The return value from the command is the result of the body script that was executed, or an empty string if none of the expressions was non-zero and there was no bodyN. EXAMPLES
A simple conditional: if {$vbl == 1} { puts "vbl is one" } With an else-clause: if {$vbl == 1} { puts "vbl is one" } else { puts "vbl is not one" } With an elseif-clause too: if {$vbl == 1} { puts "vbl is one" } elseif {$vbl == 2} { puts "vbl is two" } else { puts "vbl is not one or two" } Remember, expressions can be multi-line, but in that case it can be a good idea to use the optional then keyword for clarity: if { $vbl == 1 || $vbl == 2 || $vbl == 3 } then { puts "vbl is one, two or three" } SEE ALSO
expr(n), for(n), foreach(n) KEYWORDS
boolean, conditional, else, false, if, true Tcl if(n)
All times are GMT -4. The time now is 08:21 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy