Need Correct syntax for "if" in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Correct syntax for "if" in UNIX
# 1  
Old 12-09-2009
Need Correct syntax for "if" in UNIX

I am trying to write a simple if statement but that driving me crazySmilie with syntactical erorrs.

This is what I managed to come up

Code:
 
#!/bin/ksh
USERNAME=`whoami`
if [ "${USERNAME}" = "report" ];then
echo " you have logged in as report user"
elif ["${USERNAME}" = "extract" ]; then
echo " you have logged in as extract user"
else " I dont recognize you"
fi


can one of you explain me
1. where to use spaces and where not to?
2. some time I see double square brackets used [[ ...]] when should I use these?
3.should there be a space between variable and value in condition like
<variable><space><operator><space><value>
4.when should i use either single/double quotes around variables and values?
5. should 'then' be used after every conditinal statement ?

The code above it still gives me error
Code:
./trial.sh[9]: [extbatch:  not found.
./trial.sh[11]:  I dont recognize you:  not found.

I am using AIX and ksh. Not sure if it makes any difference
# 2  
Old 12-09-2009
Code:
 
#!/bin/ksh
USERNAME=`whoami`
if [ "${USERNAME}" = "report" ] ; then
   echo " you have logged in as report user"
elif [ "${USERNAME}" = "extract" ] ; then
   echo " you have logged in as extract user"
else 
  echo " I dont recognize you"
fi

# 3  
Old 12-09-2009
Quote:
Originally Posted by dr.house
Code:
 
#!/bin/ksh
USERNAME=`whoami`
if [ "${USERNAME}" = "report" ] ; then
   echo " you have logged in as report user"
elif [ "${USERNAME}" = "extract" ] ; then
   echo " you have logged in as extract user"
else 
  echo " I dont recognize you"
fi

@dr.House . Thank youSmilie. I now understand why ppl say. you need a fresh pair of eyes looking into your code.

I had few questions about if syntax in my post. I would be thankful if you can advice me
# 4  
Old 12-09-2009
Quote:
Originally Posted by kkb
can one of you explain me
1. where to use spaces and where not to?
2. some time I see double square brackets used [[ ...]] when should I use these?
3.should there be a space between variable and value in condition like
<variable><space><operator><space><value>
4.when should i use either single/double quotes around variables and values?
5. should 'then' be used after every conditinal statement ?
Some very general answers: -

1) Use spaces as you show in question 3, not during assignments such as X=12.
2) I think there are two versions of ksh in general use, 88 and 93? [[ ]] is ksh 88 whereas [ ] is ksh 93 Most commercial UNIX environments still use 88 somewhat inexplicably. The company I work for is only 5 years old but uses 88!!
3) Yes for conditional statements.
4) Big subject. Quoting is one of the trickiest subjects in shell scripting. On a simple level if a string contains spaces you probably want to quote it but not always.
5) In 88 at least you want to put a "then" after an if although if statements are not the only types of conditional statements,

Experiment Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. OS X (Apple)

"Permission denied" when trying to SSH my iPhone though password is correct

Hi, I hope this is the correct section in the forum to post as I'm trying to SSH from my MacBook. I was looking to see whether ssh on my jailbroken iPhone 6s (10.3.1) still works fine and was following this old reddit guide. I installed OpenSSH&OpenSSL from Cydia and changed the password using... (7 Replies)
Discussion started by: hss1
7 Replies

3. UNIX for Dummies Questions & Answers

Unix "look" Command "File too large" Error Message

I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this: $ look "string" "/home/patrick/filename.txt" However, this gives me the following message: "look: /home/patrick/filename.txt: File too large" So, I have two... (14 Replies)
Discussion started by: shishong
14 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Compare file names and select correct elements to include in "for each loop"

Hi everyone, I`ll try to be most clear I can explaining my help request. I have 2 folders Folder A-->This folder receives files through FTP constantly Folder B-->The files from Folder A are unzipped and then processed in Folder B Sometimes Folder A doesn`t contain all... (2 Replies)
Discussion started by: cgkmal
2 Replies

6. Shell Programming and Scripting

What's the correct syntax to use "if-else"

hi, i'm a beginner in Linux, actually i use andLinux, i have a data file with 11 columns 170 N SER T 25 56.962 42.701 -133.423 1.00 87.04 N 171 CA SER T 25 57.076 41.790 -132.280 1.00 86.65 C 172 C SER T 25 57.766 40.492 -132.692 1.00 87.52 C 173 O SER T 25 58.653 39.988 -131.992 1.00 86.59... (6 Replies)
Discussion started by: elsa
6 Replies

7. Shell Programming and Scripting

Pls correct the "if" syntax

Iam a learner of UNIX. Fortunately I got this site. I want to check the file for its existance. if echo " Not present" else echo "Present" fi The above code is working fine. But I also want to check for the files which are compressed. I tried the following code and it is not... (5 Replies)
Discussion started by: ganapati
5 Replies

8. UNIX for Dummies Questions & Answers

correct syntax for using "grep regex filename" ?

I'm trying to grep a long ls by looking at the beginning of each filename for example: Many files begin with yong_ho_free_2005... Many files begin with yong_ho_2005... I can't just use "grep yong_ho" otherwise It'll display both files. So I'm trying to use a regex but my syntax is wrong. ... (2 Replies)
Discussion started by: yongho
2 Replies

9. UNIX for Advanced & Expert Users

Commands on Digital Unix equivalent to for "top" and "sar" on other Unix flavour

Hi, We have a DEC Alpha 4100 Server with OSF1 Digital Unix 4.0. Can any one tell me, if there are any commands on this Unix which are equivalent to "top" and "sar" on HP-UX or Sun Solaris ? I am particularly interested in knowing the CPU Load, what process is running on which CPU, etc. ... (1 Reply)
Discussion started by: sameerdes
1 Replies
Login or Register to Ask a Question