Defining Variables
s#!/bin/bash # Define Variables p=$(pwd) one=$(ls) wi=$(whoami) d=$(date) # The brackets $(...) are necessary for command substitution. echo "Run variables tasks" echo # empty line echo $p echo $one echo $wi echo $d echo $c
Input/Output
#!/bin/bash a=$(hostname) echo echo Hi my server name is $a echo What is your name? read b echo Hi $b, Nice meeting with you.
if-else Statement
if [condition] then statement1 else statement2 fi
Example: #!/bin/bash count=100 if [ $count -eq 100 ] then echo count is 100 else echo count is NOT 100 fi #Output: mypc@mypc:~/tmp$ ./script.sh count is 100
For Loops
Keep running until a specified number or variable value is met.
For example:
- If
variable = 10
, then run the script 10 times.
- If
variable = green, blue, red
, then run the script 3 times, once for each color.
for item in list do # Code to be executed on each iteration done #Example for i in 1 2 3 4 5 do echo $i done
While Loop
The
while
loop allows you to execute a block of code as long as a certain condition is true. while condition do # Code to be executed done