if … then … fi

October 13th, 2006 mysurface Posted in Bash, echo, if, Misc | Hits: 218601 | 15 Comments »

if iis a basic programming checking for condition and reacts based on the condition, bash uses if to check for

  1. File
  2. integer
  3. String

For checking the File, let say your file name is stored at $fn, and you wanna check the file is exist ? do this:

if [  -e $fn ]
echo "$fn exist."
echo "$fn does not exist."

Bellow are the list of other common File checking,

-e, -a File exist
-f File is a regular file
-d File is a directory
-s File is non-zero size
-h, -L File is a symbolic link
-r, -w, -x Check file permission
-u File’s suid flag is set
-O File owner is you
-G File group same as yours
! “Not”, reverse

To check for integer, let say we read user input and store at $num, and check whether is it greater than 10

read num;
if [ $num -gt "10" ]
echo "$num is greater than 10"

Bellow are other common Integer checking.

-eq Is equal to
-ne Not equal to
-gt Greater than
-ge Greater or equal
-lt Less than
-le Lesser or equal

You can uses <, <=, >, >= but you need to put that into if ((…)), let say

if (($num >= "10" ))
echo "$num greater than 10"

Now check for string, uses if [[…]] instead of if[…] will reduce a lots of problem of adding escape character. Let say now i wanna compare 2 string to see whether they are similar, $str1, $str2.

if [[ $str1 = $str2 ]]
echo "They are equal."

Bellow are other common string checking

=, == Is equal to
!= Not equal to
> Greater than
< Less than
-z Null string
-n Not null string

15 Responses to “if … then … fi”

  1. Thanks alot I was looking for this information.

    End Thanks alot to open source comunity .

  2. Thank you. I’m also looking for this information

  3. I delight in, result in I found exactly what I used to be taking a look for. You have ended my 4 day lengthy hunt! God Bless you man. Have a nice day. Bye

  4. certainly like your web site but you need to take a look at the spelling on quite a few of your posts. Many of them are rife with spelling issues and I find it very troublesome to tell the reality on the other hand I’ll surely come back again.

  5. howdy! , I like your publishing extremely a lot! proportion many of us talk excess relating to your post for America online? I personally require an authority about this method to resolve our difficulty. Might be which is people! Looking onward to find out you.

  6. Good post however I was wondering if you could
    write a litte more on this subject? I’d be very grateful if you could elaborate a little bit more. Kudos!

  7. Hi, I do believe this is a great site. I stumbledupon it
    ;) I am going to revisit once again since i have book-marked it.
    Money and freedom is the greatest way to change, may you
    be rich and continue to help others.

  8. Thanks for sharing superb informations. Your web site is very cool. I am impressed by the details that you have on this site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. You, my friend, ROCK! I found simply the information I already searched everywhere and just couldn’t come across. What a perfect web-site.

  9. Have you ever thought about creating an e-book or guest authoring
    on other blogs? I have a blog based upon on the same subjects you
    discuss and would love to have you share some stories/information. I know my audience would enjoy your work.
    If you are even remotely interested, feel free to shoot me an email.

    web site [Betsey]

  10. Mable Das Says:

    Thanks for sharing useful information…

  11. I’ve been exploring for a little bit for any high-quality articles or weblog posts on this sort of house .
    Exploring in Yahoo I eventually stumbled upon this web site.
    Studying this information So i am glad to exhibit that I have
    an incredibly good uncanny feeling I discovered exactly what I needed.
    I such a lot for sure will make certain to don?t overlook this site
    and give it a look on a continuing basis.

  12. Thanks a bunch !
    You’ve made my life easier….

  13. Thanks Heaps!!

  14. Awesome! Its genuinely amazing post, I have got
    much clear idea on the topic of from this paragraph.

  15. He estado googleando un poco por artículos de alta calidad o entradas en webs sobre estos contenidos. Explorando en Google por fin encontré este blog. Con lectura de esta información, estoy convencido que he encontrado lo que estaba buscando o al menos tengo esa extraña sensacion, he descubierto exactamente lo que necesitaba. ¡Por supuesto voy hacer que no se olvide este blog y recomendarlo, os pienso visitar regularmente.


Leave a Reply