Scripting Classroomnotes 05/Feb/2023

Using Multiple Commands

  • If you want to run multiple commands together you can run them on same prompt using semi-colon and also using ampersand (&&)

Displaying Messages

  • The echo command displays a simple text string

  • Refer Here for gnu documentation

Command Substitution

  • Commands can be substitute using the following
$()
``

Output redirection

  • The basic form of output redirection is greater than >
command > outputfile 


* the >> is also used for output redirection in this if the file exists then the contents are appended and if the file doesnot exist it will create a new file and add/append contents

Input redirection

  • The basic form of input redirection is less than <
command < outputfile 


* wc command provides a count of text in the data. It produces three values
* The number of lines
* The number of words
* The number of bytes

Employing Pipes

  • Lets find the word count for all the packages installed on ubuntu
  • Linux has support of pipes where you can pass output of one command as input to other
command1 | command2

Performing Math

  • The expr command
Operator Description
ARG1 | ARG2 Return ARG1 if neither argument is null or 0, otherwise Return ARG2
ARG1 & ARG2 Return ARG1 if neither argument is null or 0, Otherwise Return 0
ARG1 < ARG2 Return 1 if ARG1 is less than ARG2
ARG1 <= ARG2 Return 1 if ARG1 is less than or equal to ARG2
ARG1 > ARG2 Return 1 if ARG1 is greater than ARG2
ARG1 >= ARG2 Return 1 if ARG1 is greater than or equal to ARG2
ARG1 + ARG2 Return the sum of ARG1 and ARG2
ARG1 - ARG2 Return the difference of ARG1 and ARG2
ARG1 / ARG2 Return the quotient of ARG1 and ARG2
ARG1 % ARG2 Return the remainder of ARG1 and ARG2
ARG * ARG2 Returns the multipe of ARG1 and ARG2
  • Lets write a script to calculate simple interst in Bash

Exit Status

  • Every command that runs in linux shell uses an exit status to indicate to the shell it has done processing. This value will be a number between 0 to 255.
  • Refer Herefor the url to view status codes



If-then command

  • Basic syntx
if command
then
   commands
fi
  • Conditonal expressions of bash Refer Here
  • Figure out conditional expressions for
    • equals of text and numbers
    • less than or greater of text and numbers
    • file exists
    • folder exists
  • If the variable is not defined assign a default value
#!/bin/bash
DEFAULT_DIST="ubuntu"
if [ -z $DIST ]; then
    echo "DIST has no value"
    DIST=$DEFAULT_DIST
fi
echo $DIST
  • If directory doesnot exists create a directory
#!/bin/bash
# if folder does not exists then create
FOLDER='/tmp/dummy'
if [ ! -d "$FOLDER" ]; then
    echo "$FOLDER does not exists so creating"
    mkdir -p $FOLDER
else 
    echo "$FOLDER exists"
fi

Exercise

  • Explain what i have done over here . /etc/os-release && echo $ID
Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a comment

Turn on JavaScript to leave a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Plugin for Social Media by Acurax Wordpress Design Studio
Exit mobile version
%%footer%%