Lecture 6 Linux Shell Scripting PDF
Document Details
Uploaded by NonViolentGyrolite3213
National University of Sciences and Technology
Dr. Absaar Ul Jabbar
Tags
Summary
This document is a lecture on Linux shell scripting, covering topics such as shell commands, variables, user input, and other related concepts. The lecture appears to be part of a computer science course at the National University of Sciences and Technology, Islamabad.
Full Transcript
Computing for CSE Shell Scripting Dr. Absaar Ul Jabbar School of Interdisciplinary Engineering and Sciences (SINES) National University of Sciences and Technology, Islamabad. 1 Du...
Computing for CSE Shell Scripting Dr. Absaar Ul Jabbar School of Interdisciplinary Engineering and Sciences (SINES) National University of Sciences and Technology, Islamabad. 1 Dua of the Day ا َٰ ُ َٰ ُ س ْب َحن ََك ِإنِى ك َ ا َ َٰ ٓا ٓ َّل ِإلهَ ِإ نت ِم َن ٱلظ ِل ِمي َن ُ نت َ َّل أ There is none worthy of Worship besides You, You are far exalted and above all weaknesses, Surely, I am from among the wrongdoers. [Surah Al-Anbiya verse 87] Hadiths of the Day Anas ibne Malik (R.A) narrates that Rasullulah (S.A.W.) said: Good tidings to the one who has believed in me and seen me; and good tidings seven times over to the one who has believed in me and has not seen me (Musnad Ahmad) Anas ibne Malik (R.A.) narrates that Rasullalah (S.A.W) said: I wish I could meet my brothers. The Sahaba asked: Are we not your brothers? Rasullalah (S.A.W.) said: You are my companions, but my brothers are those who will believe in me without seen me. (Musnad Ahmad) What is Shell Script? A shell script is a series of commands written in a file. These are read and executed by the shell program. A Text File With Instructions Executable Shell scripts are executed line by line Types of Shells To see which Shell type are supported by your OS >>> cat /etc/shells /bin/sh : Bourne Shell /bin/bash : Bourne Again Shell /bin/tcsh : TENEX C Shell /bin/ksh : Korn Shell /bin/dash : Debian Almquist Shell We will study bash shell in this course To see which shell you are using? >>> echo $SHELL /bin/bash How Do You Identify a Bash Script? File extension of.sh By convention, bash scripts end with a.sh bash scripts can run perfectly fine without the sh extension Extension helpful for the editor Scripts start with a shebang Shebang = combination of bash # and bang ! Shebang is followed by bash shell path Shebang tells the shell to execute it via bash shell. Scripts have execution rights Writing your first Shell Script >>> touch salam.sh >>> echo “#! /bin/bash” > salam.sh >>> echo “echo Salam World” >> salam.sh Change file permissions to include execute permissions >>> ls –l : To check current permissions >>> chmod u+x salam.sh : To change permissions Run the script >>>./salam.sh or >>> bash salam.sh Variables Variables are containers to store data There are two types of variables in Linux 1. System variables: Predefined by the OS Defined in Upper cases 2. User defined variables Generally, we do not define them in upper cases Examples of System variables: >>> echo $BASH :Gives the name of our shell >>> echo $BASH_VERSION :Gives the version number of your shell Variables Examples of user defined variables: >>> name = Ahmad; marks=80 >>> echo $name is a student. He scored $marks percent in exams Variable Names The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _). Valid variable names Invalid variable names Variables Read-only Variables Its value cannot be changed. Read User Input >>> #! /bin/bash >>> read –p ‘Username: ’ user_var >>> read –sp ‘Password: ’, pass_var >>> echo “Username: $user_var” >>> echo “Password: $pass_var” To store variables in an array >>> read –a names >>> echo “Names: ${names}, ${names}” To print whole array:${names[*]} Read User Input #!/bin/sh echo "What is your name?" read PERSON echo “Salam! $PERSON" Passing Arguments to a Shell Script Arguments passed are stored in $1, $2, $3,… variables …………………………………………………… #! /bin/bash echo $1 $2 $3 ……………………………………………………./myfile.sh Ali Ahmad Sara Arithmetic Expressions Operators supported by bash for mathematical calculations: If-else Statement in Shell Script Syntax: Using an if statement only: if...then...fi Using an if with an else statement: if...then...else...fi Using multiple else statements with if: if..elif..else..fi If Statement Example: If-else Statement Example: if..elif..else Statement Example: If-else Statement in Shell Script Example: To check if a file exits in a directory #!/bin/bash If [-f ~/myfile] Then echo “The file exists.” else echo “The file does not exist” fi Running Jobs in Background Long jobs can be run in the background, so that you can do other tasks You will need this when running long simulations in your research Execute a command in the background by placing an ampersand (&) on the command line at the end of the command. The user job number: the System Process number: number by which the the number by which the user references the job. system identifies the job Prints a file in the background and terminal returns the control to user for running other commands Running Jobs in Background You can place more than one command in the background. The command jobs lists the jobs being run in the background. Bring Jobs to the Foreground You can bring a job out of the background with the foreground command, fg. Cancelling Jobs running in the background: If you want to cancel a job running in the background, you can force it to end with the kill command. The kill command takes as its argument either the user job number or the system process number. The user job number must be preceded by a percent sign (%). ps and kill ps command displays process system number of all the running processes kill command can be used using the system process number to stop a process running in the background Aliases alias command instructs the shell to replace one string with another string while executing the commands. alias command can be used to give a name of your choice to any Linux command Syntax: alias new_command_name = old_command_name Aliases You can also use the name of a command as an alias. Example: In the case of the rm, cp, and mv commands, the -i option should always be used to ensure an existing file is not overwritten. Instead of always being careful to use the -i option, you can alias the command name to include the option. Aliases To print all the defined aliases >>> alias –p or >>> alias To remove an alias >>> unalias [alias name] How to permanently store aliases across sessions? We can store aliases in shell configuration files. For example, for Bash Shell, we can write in ~/.bashrc. >>> alias la=‘ls –la’ Remark: You may need to exit and restart the terminal to load updated.bashrc file