Tuesday, February 2, 2021

shell script - an example

shell script - an example - 2/02/2021

- plain text file
- run from top to bottom
- that can run on command line can run on script
-


Install nano
# yum install -y nano
# apt-get install nano -y


# nano day1.sh
#!/bin/bash
echo "Welcome to shell script!"

wq!


#! = shebang    # interpretor
if you don't define this line, it will run on default shell which may cause problem.




give execute permission. shell script must have execute permission
# chmod +x day1.sh
# ./day1.sh


find if any command is shell built-in

# type -a echo

built-in shell command


# cat day2.sh
#!/bib/bash
#
SKILL="Shell scripting"
echo "I want to learn ${SKILL}, and practice ${SKILL}."


variable name can not start with number, special characters, or -
So, variable can be
SKILL
SKILL1
_SKILL
skill
Skill

invalid
3SKILL
A-SKILL
E@Mail

to return the vale

echo ${variable} or $variable

best practice is to use curly braces

# cat day3.sh
#!/bin/bash
WORD="script"
echo "Shell {WORD}ing is fun !"

here ing is not part of variable
Run the script
# ./day3.sh


# cat day3.sh
#!/bin/bash
WORD="script"
echo "Shell {WORD}ing is fun !"
echo "Shell $WORDing is fun !!"

$WORD do not use this pattern to calling variable.
use ${WORD}

# cat day3.sh
#!/bin/bash
WORD="script"
echo "Shell {WORD}ing is fun !"
WORD="read"
echo "${WORD}ing is fun !!"

execute the shell
# sh day3.sh
Shell {WORD}ing is fun !
reading is fun !!


commenting
- comments are not executing
- explaning for easy to read and understand

# cat day3.sh
#!/bin/bash

# This is an example for variable assignment
# assigning a variable
WORD="script"
echo "Shell {WORD}ing is fun !"

# now, change the value of word variable.
WORD="read"
echo "${WORD}ing is fun !!"


# cat day4.sh


#!/bin/bash
# this script display info about system

# let user know scripting is starting
echo "Starting the sysinfo script .."

# Display host name of system
hostname

# current date and itme
date
# kernel info
uname -r
uname -m

# disk usage info
df -h

# script completed.
echo "Script completed successfully"


==================================

# cat day5.sh
#!/bin/bash
# determine if user is executing using root or not

# Display the UID
echo "Your UID is $(UID}"

# Display if the user is the root user or not

if [[ "${UID}" -eq 0 ]]
then
  echo " You are root"
else
  echo "You are not root."
fi

Note: You must have at least one space between braces and other characters.

if you are comparing or testing numbers in shell,
you have to use -eq, -gt, -ne, -lt, -ge are rhythmic text operators.

some time,  you will see comparing with single braket as well, such as

if [ "${UID}" -eq 0 ]  - old method
if [[ "${UID}" -eq 0 ]]  - new method
this is correct too but this is an old method of representing the comparision.

test your script
# sh day5.sh
Your UID is 0
 You are root


Now, lets add some fun part


# cat day5.sh
#!/bin/bash
# determine if user is executing using root or not

# Display the UID
echo "Your UID is $(UID}"

# Display if the user is the root user or not

if [[ "${UID}" -eq 0 ]]
then
  echo "Installing software ... Please wait .."
  # include command to install software
  sleep 3
  yum install httpd

else
  echo "become root to install software .."
fi





No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...