Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a...

41
scripting labs – tecmint.com 1

description

Types of Shell on a Standard Linux Distribution Bourne shell : The Bourne shell was one of the major shells used in early versions and became a de facto standard. It was written by Stephen Bourne at Bell Labs. Every Unix-like system has at least one shell compatible with the Bourne shell. The Bourne shell program name is "sh" and it is typically located in the file system hierarchy at /bin/sh. 3

Transcript of Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a...

Page 1: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

scripting labs – tecmint.com

1

Page 2: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Understanding Linux Shell• Shell: A Command-Line Interpreter that connects a user

to Operating System and allows to execute the commands or by creating text script.

• Process: Any task that a user run in the system is called a process. A process is little more complex than just a task.

• File: It resides on hard disk (hdd) and contains data owned by a user.

• X-windows aka windows: A mode of Linux where screen (monitor) can be split in small "parts" called windows, that allow a user to do several things at the same time and/or switch from one task to another easily and view graphics in a nice way.

• Text terminal: A monitor that has only the capability of displaying text stuff, no graphics or a very basic graphics display.

• Session: Time between logging on and logging out of the system.

2

Page 3: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Types of Shell on a Standard Linux Distribution

• Bourne shell : The Bourne shell was one of the major shells used in early versions and became a de facto standard. It was written by Stephen Bourne at Bell Labs.

• Every Unix-like system has at least one shell compatible with the Bourne shell.

• The Bourne shell program name is "sh" and it is typically located in the file system hierarchy at /bin/sh.

3

Page 4: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

shells continued

• C shell: The C shell was developed by Bill Joy for the Berkeley Software Distribution.

• Its syntax is modeled after the C programming language.

• It is used primarily for interactive terminal use, but less frequently for scripting and operating system control.

• C shell has many interactive commands.

4

Page 5: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Beginning the Fun! (Linux Shell)• There exist thousands of commands for command-line

user, how about remembering all of them? Hmmm! – Simply you can not. – The real power of computer is to ease the ease your work,

you need to automate the process and hence you need scripts.

• Scripts are collections of commands, stored in a file. – The shell can read this file and act on the commands as if

they were typed at the keyboard. – The shell also provides a variety of useful programming

features to make scripts truly powerful.

5

Page 6: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Basics of Shell Programming• To get a Linux shell, you need to start a terminal.• To see what shell you have, run: echo $SHELL.• In Linux, the dollar sign ($) stands for a shell variable.• The ‘echo‘ command just returns whatever you type in.• The pipeline instruction (|) comes to rescue, when chaining

several commands.• Linux commands have their own syntax, Linux won’t forgive

you whatsoever if you make a mistake. – If you get a command wrong, you won’t flunk or damage

anything, but it won’t work.• #!/bin/sh – It is called shebang. It is written at the top of a

shell script and it passes the instruction to the program /bin/sh.

6

Page 7: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

About shell Script

• Shell script is just a simple text file with ".sh" extension, having executable permission.

7

Page 8: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Process of writing and executing a script

• Open terminal.• Navigate to the place where you want to create script

using ‘cd‘ command.• cd (enter) [This will bring the prompt at Your home

Directory].• touch hello.sh (Here we named the script as hello,

remember the ‘.sh‘ extension is compulsory).• vi hello.sh (nano hello.sh) [You can use your favorite

editor, to edit the script].• chmod 744 hello.sh (making the script executable).• sh hello.sh or ./hello.sh (running the script)

8

Page 9: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

./ versus sh• When you run any script by passing the filename to the script interpreter

program, you are running the interpreter program with the script as an argument passed into it. – For example this would look like the process 'sh' with the argument

'filename.sh'. The sh interpreter is opening the file.• On the other hand if you run the script itself, the system calls out to the

interpreter program specified and feeds in the scripts contents. In this case the process looks like 'filename.sh' with no arguments.

• You should make sure you have a bang line:• #!/bin/bash

– A bang line is the very first line in the script and starts with the same two characters #!

– these are what the system reads when it tries to execute the script and then the system passes the script to the program immediately after. Note that this line isn't anything to do with bash and works just as well for python and perl, even though they're very different languages.

– You would use #!/usr/bin/python for example and then follow it with python code.

9

Page 10: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

./ versus sh• For your specific script either way will work, – except that ./script.sh requires execution and

readable bits– while bash script.sh only requires readable bit.

• The reason of the permissions requirement difference lies in how the program that interprets your script is loaded:– ./script.sh makes your shell run the file as if it was a

regular executable.– sh script.sh makes your shell run bourne and

pass script.sh as the command-line argument

10

Page 11: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Writing your First Script

#!/bin/bash # My first script echo "Hello World!"• Save the above lines in a text file, make it

executable and run it• #!/bin/bash (is the shebang.) • # My first script (is comment, anything following '#' is a comment) • echo "Hello World!" (is the main part of this script)

Hello World!

11

Page 12: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Writing your Second Script• OK time to move to the next script. This script will tell

your "username" and list the running processes.#! /bin/bash echo "Hello $USER" echo "Hey I am" $USER "and will be telling you about the

current processes" echo "Running processes List" ps• Create a file with above code, save it to anything you

want, but with extension ".sh", make it executable and run it, from you terminal.

12

Page 13: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Sample Output

Hello user34 Hey I am user34 and will be telling you about

the current processes Running processes List PID TTY TIME CMD 1111 pts/0 00:00:00 bash 1287 pts/0 00:00:00 sh 1288 pts/0 00:00:00 ps

13

Page 14: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Was this cool?• Writing script is as simple as getting an idea and writing

commands. • There are some restrictions, too.

– Shell scripts are excellent for concise file system operations and scripting the combination of existing functionality in filters and command line tools via pipes.

• When your needs are greater – whether

in functionality, robustness, performance, efficiency etc– then you can move to a more full-featured language.

• If you already know C/Perl/Python programming language or any other programming language, learning the scripting language won’t be much difficult.

14

Page 15: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Writing your Third Script

• This script acts as an interactive script. #! /bin/bash echo "Hey what's Your First Name?"; read a; echo "welcome Mr./Mrs. $a, would you like to tell us, Your Last Name"; read b; echo "Thanks Mr./Mrs. $a $b for telling us your name"; echo "*******************" echo "Mr./Mrs. $b, it's time to say you good bye"

15

Page 16: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Sample OutputHey what's Your First Name? Avishek welcome Mr./Mrs. Avishek, would you like to tell us, Your Last Name Kumar Thanks Mr./Mrs. Avishek Kumar for telling us your name********************************************** Mr./Mrs. Kumar, it's time to say you good bye

16

Page 17: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

; versus ;;• Single semicolons at the end of a line are superfluous, since the newline is also a

command separator. • case specifically needs double semicolons at the end of the last command in each

pattern block.case $1 in 0 ) echo $1 = 0; OUTPUT=3;; 1 ) echo $1 = 1; OUTPUT=4;; 2 ) echo $1 = 2; OUTPUT=4;; esac HID=$2; BUNCH=16; LR=.008;

17

Page 18: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

just try it

• To Learn something you need to do it, without the fear of being unsuccessful.

• We have an environment that can be quickly rebuilt

18

Page 19: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Interpreting the following script

• Most of the ‘key words‘ would be known to you and most of them are self explanatory.

• e.g., MAX sets the maximum value of the variable,

• for is a loop and anything within the loop keeps on executing again and again till the loop is invalid for given value of input.

19

Page 20: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Script 1: Drawing a Special Pattern#!/bin/bash MAX_NO=0 echo -n "Enter Number between (5 to 9) : " read MAX_NO if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then echo "WTF... I ask to enter number between 5 and 9, Try Again" exit 1 fi clear for (( i=1; i<=MAX_NO; i++ )) do for (( s=MAX_NO; s>=i; s-- )) do echo -n " " done for (( j=1; j<=i; j++ )) do echo -n " ." done echo "" done continued on the next slide

20

Page 21: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

special pattern continued###### Second stage ###################### for (( i=MAX_NO; i>=1; i-- )) do for (( s=i; s<=MAX_NO; s++ )) do echo -n " " done for (( j=1; j<=i; j++ )) do echo -n " ." done echo "" done echo -e "\n\n\t\t\t Whenever you need help, Tecmint.com is always there"

21

Page 22: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Output[root@tecmint ~]# chmod 755 Special_Pattern.sh [root@tecmint ~]# ./Special_Pattern.sh Enter Number between (5 to 9) : 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Whenever you need help, Tecmint.com is always there

22

Page 23: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

a colorful script• Who says, Linux is colorless and boring, save the codes

below to anything [dot] sh, make it executable and Run it.

#!/bin/bash clear echo -e "\033[1m Hello World" # bold effect echo -e "\033[5m Blink" # blink effect echo -e "\033[0m Hello World" # back to normal echo -e "\033[31m Hello World" # Red color echo -e "\033[32m Hello World" # Green color echo -e "\033[33m Hello World" # See remaining on screen echo -e "\033[34m Hello World" echo -e "\033[35m Hello World" echo -e "\033[36m Hello World" echo -e -n "\033[0m" # back to normal echo -e "\033[41m Hello World" echo -e "\033[42m Hello World" echo -e "\033[43m Hello World" echo -e "\033[44m Hello World" echo -e "\033[45m Hello World" echo -e "\033[46m Hello World" echo -e "\033[0m Hello World“

https://www.npmjs.com/package/ansi-codes

[root@tecmint ~]# chmod 755 Colorfull.sh[root@tecmint ~]# ./Colorfull.sh Hello World Blink Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World

23

Page 24: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Encrypt a File/Directory• This script will encrypt a file • (remember? directory/driver/link/file….

everything is treated as file, in Linux). • The current limitation of the above script is that

it doesn’t support auto completion of name using TAB. Moreover, you need to place the script and file to be encrypted in the same folder.

• You may need to install “pinentry-gui”, using yum or apt the package, if required.

• [root@midstage ~]# yum install pinentry-gui [root@midstage ~]# apt-get install pinentry-gui

24

Page 25: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

encrypt.sh

• Crete a file called “Encrypt.sh”, make it executable and run it as shown.

#!/bin/bash echo "Welcome, I am ready to encrypt a file/folder for you" echo "currently I have a limitation, Place me in the same folder,

where a file to be encrypted is present" echo "Enter the Exact File Name with extension" read file; gpg -c $file echo "I have encrypted the file successfully..." echo "Now I will be removing the original file" rm -rf $file

25

Page 26: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

encrypt.sh

• gpg -c : • This will encrypt your file, using a passkey

aka password. • So after encrypting a file what you need?

Obviously! decrypting the file. Note: gpg -d filename.gpg > filename is what

you need to implement in your decryption script.

26

Page 27: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Checking Server Utilization

• Checking the server utilization is one of the important task of an administrator

• a good administrator is one who knows how to automate their day to day tasks.

• Below is the script that will give many such information about your server.

27

Page 28: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Checking Server Utilization#!/bin/bash date; echo "uptime:" uptime echo "Currently connected:" w echo "--------------------" echo "Last logins:" last -a |head -3 (last logged in with hostname, 3 lines)echo "--------------------" echo "Disk and memory usage:" df -h | xargs | awk '{print "Free/total disk: " $11 " / " $9}' free -m | xargs | awk '{print "Free/total memory: " $17 " / " $8 " MB"}' echo "--------------------" start_log=`head -1 /var/log/messages |cut -c 1-12` oom=`grep -ci kill /var/log/messages` echo -n "OOM errors since $start_log :" $oom

http://linux.die.net/man/1/nmaphttp://www.binarytides.com/linux-ss-command/

echo "" echo "--------------------" echo "Utilization and most expensive processes:" top -b |head -3 echo top -b |head -10 |tail -4 echo "--------------------" echo "Open TCP ports:" nmap -p- -T4 127.0.0.1 echo "--------------------" echo "Current connections:" ss -s echo "--------------------" echo "processes:" ps auxf --width=200 echo "--------------------" echo "vmstat:" vmstat 1 5

last –a logged in with ip head -3 number linesdf –h human readable xargs – read from standinfree – display memory cut – remove sectionsgrep – count files with matches top - display tasksnmap – network explore port scannerss – socket statistics – summary statisticsvmstat – virtual memory statistics

28

Page 29: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Check Disk Space and Sends an Email Alert

• How about getting an email when disk use in partition PART is bigger than Maximum allowed, it is a life saver script for web administrators with little modification.

MAX=95 [email protected] PART=sda1 USE=`df -h |grep $PART | awk '{ print $5 }' | cut -d'%' -f1` if [ $USE -gt $MAX ]; then echo "Percent used: $USE" | mail -s "Running out of disk space" $EMAIL fi

Note: Remove “USER” with your user name. You can check mail using the ‘mail‘ command.

29

Page 30: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Bash Keywords• A keyword is a word or symbol that has a special meaning to a

computer language. • The following symbols and words have special meanings

to Bash when they are unquoted and the first word of a command.! esac select } case fi then [[ do for until ]] done functionwhile elif if time else in {

• Unlike most computer languages, Bash allows keywords to be used as variable names even though this can make scripts difficult to read.

• To keep scripts understandable, key-words should not be used for variable names.

30

Page 31: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

bash keywords continued

• A command is implemented in shell as $(command). You might have to include the full path of command. e.g.,$(/bin/date), for correct execution.

• You may find the path of specific program using ‘whereis‘ command. e.g., whereis date

[root@tecmint /]# whereis date date: /bin/date /usr/share/man/man1/date.1.gz

31

Page 32: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Move Current Working Directory• Move from current working directory to any level up by just

providing the numerical value at the end of script while executing.#! /bin/bash LEVEL=$1 for ((i = 1; i <= LEVEL; i++)) do CDIR=../$CDIR done cd $CDIR echo "You are in: "$PWD exec /bin/bash

• Save the above codes as “up.sh“, on your desktop. Make it executable (chmod 755 up.sh). Run:

• ./up.sh 2 (will Move the current working directory to two level up)../up.sh 4 (will Move the current working directory to four level up).

32

Page 33: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Use and Area of Application

• In larger scripts which contains folder inside folder inside… containing libraries, binaries, icons, executables, etc at different location, You as a developer can implement this script to move to the desired location in a very automated fashion.

• Note: For is a loop in the above script and it will continue to execute till the values are not true for the loop.

33

Page 34: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Sample Output

• [root@tecmint /]# chmod 755 up [root@tecmint /]# ./up.sh 2 You are in: / [root@tecmint /]# ./up.sh 4 You are in: / [root@tecmint /]#

• user34@SAVAGE:~/apache-tomcat-7.0.65/bin$ pwd/home/user34/apache-tomcat-7.0.65/binuser34@SAVAGE:~/apache-tomcat-7.0.65/bin$ ../../up.sh 2You are in: /home/user34

34

Page 35: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Create a Random File or Folder

• Create a random file (folder) with no chance of duplication.

#! /bin/bash echo "Hello $USER"; echo "$(uptime)" >> "$(date)".txt echo "Your File is being saved to $(pwd)”

• This is a Simple script but it’s working is not that much simple.

35

Page 36: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Create a Random File or Folder• This is a Simple script but it’s working is not that

simple.• ‘echo‘ : Prints everything written within the quotes.• ‘$‘ : Is a shell variable.• ‘>>‘ : The output is redirected to the output

of date command followed by txt extension.• We know the output of date command is date,

and time in hour, minute, second along with year. Hence we could get output on an organized file name without the chance of filename duplication.

• It could be very useful when user needs the file created with time stamp for future reference.

36

Page 37: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

sample output[root@tecmint /]# ./randomfile.sh Hello server Your File is being saved to /home/server/Desktop

• You can view the file which is created on desktop with Today’s Date and current time.

[root@tecmint /]# nano Sat\ Jul\ 20\ 13\:51\:52\ IST\ 2013.txt 13:51:52 up 3:54, 1 user, load average: 0.09, 0.12, 0.08

37

Page 38: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Script to Converts UPPERCASE to lowercase

• A script that converts UPPERCASE to lowercase and redirects the output to a text file “small.txt” which can be modified as required.#!/bin/bash echo -n "Enter File Name : " read fileName if [ ! -f $fileName ]; then echo "Filename $fileName does not exists" exit 1 fi tr '[A-Z]' '[a-z]' < $fileName >> small.txt

• This above script can convert the case of a file of any length with a single click from uppercase to lowercase and vice-versa if required, with little modification.

38

Page 39: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

simple calculator#! /bin/bash clear sum=0 i="y" echo " Enter one no." read n1 echo "Enter second no." read n2 while [ $i = "y" ] do echo "1.Addition" echo "2.Subtraction" echo "3.Multiplication" echo "4.Division" echo "Enter your choice" read ch

case $ch in 1)sum=`expr $n1 + $n2` echo "Sum ="$sum;; 2)sum=`expr $n1 - $n2` echo "Sub = "$sum;; 3)sum=`expr $n1 \* $n2` echo "Mul = "$sum;; 4)sum=`expr $n1 / $n2` echo "Div = "$sum;; *)echo "Invalid choice";; esac echo "Do u want to continue (y/n)) ?" read i if [ $i != "y" ] then exit fi done

39

Page 40: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

Sample Output[root@tecmint /]# ./simplecalc.sh Enter one no. 12 Enter second no. 14 1.Addition 2.Subtraction 3.Multiplication 4.Division Enter your choice 1 Sum =26 Do u want to continue (y/n)) ? y 1.Addition 2.Subtraction 3.Multiplication 4.Division Enter your choice 3 mul = 14812 Do u want to continue (y/n)) ? n

40

Page 41: Scripting labs – 1. Understanding Linux Shell Shell: A Command-Line Interpreter that connects a user…

that’s it for now

41