1 / 12

Bash Scripting Examples for String and Numeric Comparisons

Learn how to perform string and numeric comparisons in Bash scripts with detailed examples and explanations. Improve your scripting skills now!

Download Presentation

Bash Scripting Examples for String and Numeric Comparisons

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Experiment No 8 Presented by, Mr. SatishPise

  2. Test command string comparison

  3. Example 1:testing string equality using = #cat > testdemo #!/bin/bash # testing string equality testuser=rich if [ $USER = $testuser ] then echo "Welcome $testuser" fi # shtestdemo

  4. Example 2:testing string using != # cat > testdemo2 #!/bin/bash # testing string equality testuser=baduser if [ $USER != $testuser ] then echo "This isn’t $testuser" else echo "Welcome $testuser" fi # shtestdemo2

  5. Example 3ng string comparisons # cat > badtest #!/bin/bash # mis-using string comparisons val1=baseball val2=hockey if [ $val1 > $val2 ] then echo "$val1 is greater than $val2" else echo "$val1 is less than $val2" fi $ shbadtest $ ls -l hockey

  6. Example 4:testing string sort order $ cat > test9b #!/bin/bash # testing string sort order val1=Testing val2=testing if [ $val1 \> $val2 ] then echo "$val1 is greater than $val2" else echo "$val1 is less than $val2" fi $ sh test9b Testing is less than testing $ sort testfile

  7. Numeric comparisons

  8. Example 1:using numeric test comparisons # cat > test5 #!/bin/bash # using numeric test comparisons val1=10 val2=11 if [ $val1 -gt 5 ] then echo "The test value $val1 is greater than 5" fi if [ $val1 -eq $val2 ] then echo "The values are equal" else echo "The values are different" fi echo The first test condition: if [ $val1 -gt 5 ] echo tests if the value of the variable val1 is greater than 5. The second test condition: if [ $val1 -eq $val2 ] echo There is a limitation to the test numeric conditions though. Try this script: #sh test5

  9. Example 2: $ cat > test6 #!/bin/bash # testing floating point numbers val1=` echo "scale=4; 10 / 3 " | bc` echo "The test value is $val1" if [ $val1 -gt 3 ] then echo "The result is larger than 3" fi $ sh test6

  10. Testing File Comparison

  11. Checking directories Example 1: # cat > test11 #!/bin/bash if [ -d $HOME ] then echo "Your HOME directory exists" cd $HOME ls -a else echo "There’s a problem with your HOME directory" fi #sh test11

  12. Checking for a file $ cat > test13 #!/bin/bash # check if a file if [ -e $HOME ] then echo "The object exists, is it a file?" if [ -f $HOME ] then echo "Yes, it’s a file!" else echo "No, it’s not a file!" if [ -f $HOME/.bash history ] then echo "But this is a file!" fi fi else echo "Sorry, the object doesn’t exist" fi $ sh test13

More Related