#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
LINUX Shell Scripting Advanced Issues
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Introduction to UNIX / Linux - 11
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Linux Operations and Administration
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Module 9 Basic Scripting.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
LINUX programming UNIT-2 1. INDEX UNIT-IV PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Working with Bourne shell L1 1-2.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Writing Scripts Hadi Otrok COEN 346.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
Lab 8 Overview Apache Web Server. SCRIPTS Linux Tricks.
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
If condition1 then statements elif condition2 more statements […] else even more statements fi.
Group, group, group One after the other: cmd1 ; cmd2 One or both: cmd1 && cmd2 Only one of them: cmd1 || cmd2 Cuddling (there):( cmd1 ; cmd2 ) Cuddling.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
#!/bin/sh 2002/07/26 Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Computer Programming Batch & Shell Programming 9 th Lecture 김현철 컴퓨터공학부 서울대학교 2009 년 가을학기.
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Shell Script Assignment 1.
What is Bash Shell Scripting?
Pepper (Help from Dr. Robert Siegfried)
UNIX Reference Sheets CSE 2031 Fall 2010.
Linux Shell Script Programming
Presented by, Mr. Satish Pise
Shell Control Structures
Chapter 5 The Bourne Shell
Shell Control Structures
Chapter 5 Bourne Shells Scripts
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh

Step 1: vi Firstshellscript.sh Step 2: All shell scripts should begin with "#!/bin/bash" or whatever other shell you prefer. This line is called the shebang, and although it looks like a comment, it's not: it notifies the shell of the interpreter to be used for the script. Step 3: #!/bin/sh echo "Hello World" Step 4: chmod 744 Firstshellscript.sh Step 5: bash$./Firstshellscript.sh Hello World If you want to see the execution step-by-step - which is very useful for troubleshooting - then execute it with the '-x' ('expand arguments') option: sh -x Firstshellscript.sh + echo 'Hello World' Hello World

Comments in a Shell In shell scripting, all lines beginning with # are comments. # This is a comment line. # This is another comment line.

You can also have comments that span multiple lines by using a colon and single quotes: : ‘ This is a comment line. Again, this is a comment line. My God, this is yet another comment line. ’ Note: This will not work if there is a single quote mark within the quoted contents.

System Variables System variables are defined and kept in the environment of the parent shell bash$ SCRIPT_PATH=/home/blessen/shellscript bash$ export SCRIPT_PATH

User-Defined Variables These are the variables that are normally used in scripting bash$ define_tempval=blessen bash$ echo $define_tempval blessen Output -

Command line Arguments These variables are accessed using $1, $2,... $n, where $1 is the first command-line argument, $2 is the second command-line argument. $0 is the name of the script. $# will display the number of command-line arguments

#!/bin/sh echo "The first variable is $1" echo "The second variable is $2" Output : The first variable is blessen The second variable is lijoe bash$./commandline.sh blessen lijoe commandline.sh

Exit status variable This variable tells us if the last command executed was successful or not. Create a file named "test", bash$ touch test. Then, "display" the content of the file: bash$ cat test Then, check the value of $?. bash$ echo $? 0 The value 0 shows that the above command was successful. The value 1 shows that the above command was unsuccessful.

#!/bin/sh display( ) { local local_var=100 global_var=blessen echo "local variable is $local_var" echo "global variable is $global_var" } echo "======================" display echo "=======outside ========" echo "local variable outside function is $local_var" echo "global variable outside function is $global_var"

Input and Output in Shell Scripting For accepting input from the keyboard, we use read. This command will read values typed from the keyboard, and assign each to the variable specified for it. read For output, we use the echo command. echo "statement to be displayed"

Arithmetic Operations in Shell Scripting Like other scripting languages, shell scripting also allows us to use arithmetic operations such as addition, subtraction, multiplication, and division. To use these, one uses a function called expr ; "expr a + b" means 'add a and b'. e.g.: sum=`expr ` $[expression operation statement] e.g.: echo $[ ]

Conditional Statements #!/bin/sh echo "Enter your username:“ read username if [ "$username" = "blessen" ] then echo 'Success!!! You are now logged in.' else echo 'Sorry, wrong username.' fi

if [... ] then # if-code else # else-code fi if [ something ]; then echo "Something" elif [ something_else ]; then echo "Something else" else echo "None of the above" fi

Variable Comparison In shell scripting we can perform variable comparison. If the values of variables to be compared are numerical, then you have to use these options: -eq Equal to -ne Not Equal to -lt Less than -le Less than or equal to -gt Greater than -ge Greater then or equal to

The "for" Loop for ((initialization; condition; increment/decrement)) do...statements... done #!/bin/sh for (( i=1; $i <= 10; i++ )) do echo $i done

The "for" Loop for in do...statements... done

#!/bin/sh count=0 for i in `cat /etc/group` do count=`expr "$count" + 1` echo "Line $count is being displayed" echo $i done echo "End of file"

While Loop The "while" loop is another useful loop used in all programming languages; it will continue to execute until the condition specified becomes false. while [ condition ] do...statement... done

#!/bin/sh num=1 while [$num -lt 5]; do num=$[$num + 1]; echo $num; done

Select and Case Statement select in do...statements... done Syntax of case: case $ in ) statements ;; *) echo "Sorry, wrong option" ;; esac

#!/bin/bash echo "***********************" select opt in apache named sendmail do case $opt in apache) /etc/rc.d/init.d/httpd restart;; named) /etc/rc.d/init.d/named restart;; sendmail) /etc/rc.d/init.d/sendmail restart;; *) echo "Nothing will be restarted" esac echo "***********************“ # If this break is not here, then we won't get a shell prompt. break done

Syntax for "function" is: () { # start of function statements } # end of function Functions

#!/bin/sh sumcalc () { sum=$[$1 + $2] } echo "Enter the first number:" read num1 echo "Enter the second number:" read num2 sumcalc $num1 $num2 echo "Output from function sumcalc: $sum"

To view current process use the ps command:ps command ps ps aux | less ps aux | grep "process-name" ps aux | grep "httpd" ps alx | grep "mysqld" To display a tree of processes use the pstree command:pstree command pstree

pgrep - Look up processes pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to screen. List the processes called php-cgi AND owned by vivek user. pgrep -u vivek php-cgi To list the processes owned by vivek OR krish. pgrep –u vivek,krish

kill - send a signal to a process The default signal for kill is TERM. To list available signals, enter: kill -l

To send a kill signal to PID # 1234 use: kill OR kill -KILL 1234 OR kill -SIGKILL 1234

To terminate all firefox process (child and parent), enter: killall processName killall firefox-bin To send a KILL signal to firefox, enter: killall -s SIGKILL firefox-bin

drive_space The drive_space function will use the df command to provide a summary of the space used by all of the mounted file systems.df home_space The home_space function will display the amount of space each user is using in his/her home directory. It will display this as a list, sorted in descending order by the amount of space used. du -s /home/* | sort -nr uptime