AI DevOps

Bash Scripting for IT Professionals

This article covers basic and intermediate bash scripting concepts. Each section explains the command prompt instructions to help IT professionals understand and execute these scripts effectively.

#!/bin/bash

This line is called a “shebang” and indicates that the script should be run in the Bash shell.

echo ${name}

The echo command prints the value of the variable name. If name is not set, it will print an empty line.

echo "hello world"

This command prints the string “hello world” to the terminal.

MY_MESSAGE="Hello World"
echo $MY_MESSAGE
echo ${MY_MESSAGE}
  1. MY_MESSAGE="Hello World" sets a variable MY_MESSAGE with the value “Hello World”.
  2. echo $MY_MESSAGE prints the value of MY_MESSAGE.
  3. echo ${MY_MESSAGE} also prints the value of MY_MESSAGE. Using ${} is optional unless the variable name is followed by characters that are part of the string.
read MY_NAME
echo "Hello $MY_NAME - hope you're well."
  1. read MY_NAME waits for the user to input a value and assigns it to MY_NAME.
  2. echo "Hello $MY_NAME - hope you're well." prints a personalized greeting using the value entered by the user.
expr 1 + 1

The expr command evaluates expressions. Here, it calculates 1 + 1 and outputs 2.

sed -e 's/string_source$/string_target/g' test.txt

The sed command is used for text substitution. This command replaces all occurrences of string_source at the end of lines with string_target in the file test.txt.

awk -F", " '{ print $1 }' test.txt
awk -F", " '{ print $0 }' test.txt
  1. awk -F", " '{ print $1 }' test.txt uses awk to print the first field of each line in test.txt, assuming fields are separated by a comma and a space.
  2. awk -F", " '{ print $0 }' test.txt prints the entire line.
echo $1
echo $2
  1. echo $1 prints the first argument passed to the script.
  2. echo $2 prints the second argument passed to the script.
echo $0

This prints the name of the script itself.

echo $?

This prints the exit status of the last executed command. 0 indicates success, and any other number indicates an error.

function fun1() {
  echo $1
}
fun1 val1 val2
  1. This defines a function fun1 that prints its first argument.
  2. fun1 val1 val2 calls the function with val1 as the first argument and val2 as the second. It will print val1.
name=$1
if [ $name = "ali" ]; then
  echo "ok"
elif [ 1 -eq 1 ]; then
  echo "ok"
else
  echo "ok"
fi
  1. name=$1 assigns the first argument passed to the script to the variable name.
  2. The if statement checks if name is “ali” and prints “ok” if true.
  3. The elif checks if 1 is equal to 1, which is always true, and prints “ok”.
  4. The else block runs if none of the above conditions are met, printing “ok”.
for i in 1 2 3 4;
do
  echo $i
done

This loop iterates over the numbers 1 to 4, printing each number in turn.

Ali Imran
Over the past 20+ years, I have been working as a software engineer, architect, and programmer, creating, designing, and programming various applications. My main focus has always been to achieve business goals and transform business ideas into digital reality. I have successfully solved numerous business problems and increased productivity for small businesses as well as enterprise corporations through the solutions that I created. My strong technical background and ability to work effectively in team environments make me a valuable asset to any organization.
https://ITsAli.com

Leave a Reply