Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- not123=456
- name="Scott Bot"
- # Test if not123 is not equal to 123
- # Operators such as >= <= < > and == are also able to be used
- if (( $not123 != 123 ))
- then
- echo -e "This is an integer test. Notice how the conditional statement is encapsulated in '((' and '))'s\n"
- elif (( $not123 > 123 ))
- then
- echo "This is an else if statement."
- else
- echo "This happens if nothing else happens"
- fi
- # Test if 'name' is equal to 'Scott Bot'
- # Operators such as >= <= < > and == are also able to be used
- if [[ $name == "Scott Bot" ]]
- then
- echo "This is a string test. Notice the '[[' & ']]'s encapsulating the if statement"
- fi
- # Print 'file.txt exists!' if file.txt exists
- if [ -e file.txt ]
- then
- echo "file.txt exists!"
- fi
- # Print 'The directory exists!' if 'directory/' exists and is a directory rather then a file
- if [ -d /directory/ ]
- then
- echo "The directory exists!"
- fi
- # Print 'The file exists!' if 'directory/' exists and is a file rather then a directory
- if [ -f file ]
- then
- echo "The file exists!"
- fi
- # Print 'The TEXT file exists!' if 'text.txt' exists and is character encoded (is a text file)
- if [ -c text.txt ]
- then
- echo "The TEXT file exists!"
- fi
- # Print 'The BINARY file exists!' if 'exec.exe' exists and is NOT character encoded (anything but a text file)
- if [ -b exec.exe ]
- then
- echo "The BINARY file exists!"
- fi
- # Print 'File is not empty' if file.txt contains content
- if [ -s file.txt ]
- then
- echo "File is not empty!"
- fi
- # Print "File is able to be executed" is file.exe has permission to be executed
- if [ -x file.exe ]
- then
- echo "File is able to be executed"
- fi
- # Print "File is able to be read" is file.exe has permission to be read
- if [ -r file.exe ]
- then
- echo "File is able to be read"
- fi
- # Print "File is able to be written to" is file.exe has permission to be written to
- if [ -w file.exe ]
- then
- echo "File is able to be written to"
- # Get number from user
- read number
- # If number specified by user is in range [0, 100], echo 'The number is in range'
- # Notice the '&&' inside the double parentheses. They can also be used inside of double square brackets.
- if (( number >= 0 && number <= 100 ))
- then
- echo "The number is in range"
- fi
- # Get string from user
- read string
- # If the string is one of your 'names' then print 'Congrats, you guessed one of my names!'
- if [[ string == "quantum" || string == "ech" ]]
- then
- echo "Congrats, you guessed one of my names!"
- fi
- # '[[]]', '(())' and '[]' are just delineated expressions that evaluate to a Boolean value
- # The 'if' construct could easily be written as
- if true
- then
- echo "This will always run"
- fi
- # Because if statements simply get passed Boolean values like all languages.
- # This implies that you can also write the same 'string' variable comparison as
- if [[ string == "quantum" ]] || [[ string == "ech" ]]
- then
- echo "Congrats, you guessed one of my names!"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement