Advertisement
quantumech

Untitled

May 9th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.90 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. not123=456
  4. name="Scott Bot"
  5.  
  6. # Test if not123 is not equal to 123
  7. # Operators such as >= <= < > and == are also able to be used
  8. if (( $not123 != 123 ))
  9. then
  10.     echo -e "This is an integer test. Notice how the conditional statement is encapsulated in '((' and '))'s\n"
  11. elif (( $not123 > 123 ))
  12. then
  13.     echo "This is an else if statement."
  14. else
  15.     echo "This happens if nothing else happens"
  16. fi
  17.  
  18. # Test if 'name' is equal to 'Scott Bot'
  19. # Operators such as >= <= < > and == are also able to be used
  20. if [[ $name == "Scott Bot" ]]
  21. then
  22.     echo "This is a string test. Notice the '[[' & ']]'s encapsulating the if statement"
  23. fi
  24.  
  25. # Print 'file.txt exists!' if file.txt exists
  26. if [ -e file.txt ]
  27. then
  28.     echo "file.txt exists!"
  29. fi
  30.  
  31. # Print 'The directory exists!' if 'directory/' exists and is a directory rather then a file
  32. if [ -d /directory/ ]
  33. then
  34.     echo "The directory exists!"
  35. fi
  36.  
  37. # Print 'The file exists!' if 'directory/' exists and is a file rather then a directory
  38. if [ -f file ]
  39. then
  40.     echo "The file exists!"
  41. fi
  42.  
  43. # Print 'The TEXT file exists!' if 'text.txt' exists and is character encoded (is a text file)
  44. if [ -c text.txt ]
  45. then
  46.     echo "The TEXT file exists!"
  47. fi
  48.  
  49. # Print 'The BINARY file exists!' if 'exec.exe' exists and is NOT character encoded (anything but a text file)
  50. if [ -b exec.exe ]
  51. then
  52.     echo "The BINARY file exists!"
  53. fi
  54.  
  55. # Print 'File is not empty' if file.txt contains content
  56. if [ -s file.txt ]
  57. then
  58.     echo "File is not empty!"
  59. fi
  60.  
  61. # Print "File is able to be executed" is file.exe has permission to be executed
  62. if [ -x file.exe ]
  63. then
  64.     echo "File is able to be executed"
  65. fi
  66.  
  67. # Print "File is able to be read" is file.exe has permission to be read
  68. if [ -r file.exe ]
  69. then
  70.     echo "File is able to be read"
  71. fi
  72.  
  73. # Print "File is able to be written to" is file.exe has permission to be written to
  74. if [ -w file.exe ]
  75. then
  76.     echo "File is able to be written to"
  77.  
  78. # Get number from user
  79. read number
  80.  
  81. # If number specified by user is in range [0, 100], echo 'The number is in range'
  82. # Notice the '&&' inside the double parentheses. They can also be used inside of double square brackets.
  83. if (( number >= 0 && number <= 100 ))
  84. then
  85.     echo "The number is in range"
  86. fi
  87.  
  88. # Get string from user
  89. read string
  90.  
  91. # If the string is one of your 'names' then print 'Congrats, you guessed one of my names!'
  92. if [[ string == "quantum" || string == "ech" ]]
  93. then
  94.     echo "Congrats, you guessed one of my names!"
  95. fi
  96.  
  97. # '[[]]', '(())' and '[]' are just delineated expressions that evaluate to a Boolean value
  98. # The 'if' construct could easily be written as
  99. if true
  100. then
  101.     echo "This will always run"
  102. fi
  103. # Because if statements simply get passed Boolean values like all languages.
  104.  
  105. # This implies that you can also write the same 'string' variable comparison as
  106. if [[ string == "quantum" ]] || [[ string == "ech" ]]
  107. then
  108.     echo "Congrats, you guessed one of my names!"
  109. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement