Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
22 views
7 pages
Exp 1
Uploaded by
Xtr Gbu
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save exp.1 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
22 views
7 pages
Exp 1
Uploaded by
Xtr Gbu
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save exp.1 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save exp.1 For Later
You are on page 1
/ 7
Search
Fullscreen
Experiment 1 Introduction to Script Programming: Shell Expansion | Objectives: | Displaying shell expansion using echo command. @ Using control operators. @ Shell variables. @ Shell Expansion: ® Displaying shell expansion using echo command: echo command is used to demonstrate shell features. It echoes the input that it receives. Syntax of echo statement Ech ptions [strings] Example: echo Good Morning Good Morning - White Space Removal Parts that are separated by one or more consecutive white spaces (or tabs) are considered separate arguments, any white space is removed, Example1: echo Hello world Hello world Example2: echoHello world ~ White Spaces Hello world Example3: echo Hello world Hello world - Single and Double Quotes You can prevent the removal of white spaces by quoting the spaces, The contents of the quoted string are considered as one argument. Examples:echo ‘Hello world Hello world echo ‘Hello world’ ‘Hello world’ echo "Hello world Double quotes” Hello world Double quotes” Quoted lines can include special escaped characters recognized by the echo command (when using echo -e). The example below shows how to use \n for a newline, \tfor a horizontal tab (usually eight white spaces), and option \v for vertical tab with backspace interpreter ~e to have vertical tab space. Examples: echo -e "A line with \na newline” Aline with a newline echo -e ‘A line with \na newline’ Aline with a newline echo -e "A line with \ta tab" Aline with atab echo -e "A line with \ta tab" Aline with atab echo -e "\vA \vline with \va tab" A line with atab @ List the files by Echo To display all the files / folders using echo statement(like Is command), you can use the following: echo * It will display the files/folders on the working directory Example: display the files of extension .txt echo *.txt © Control Operators: The control operators are used to put more than one command on the command line, ® Semicolon ; By a semicolon one can put two or more commands on the same line separated - All the arguments before this *f ‘4. The shell will scan the line until it reaches the ';will be considered a separate command from all the arguments after the ''. Both series will be executed sequentially with the shell waiting for each ommand to finish before starting the next one. Example echo Hello; echo World Hello World ® Dollar question mark $? The exit code of the previous command is stored in the shell variable ‘S?" Actually, '$?' is a shell parameter and not a variable, since you cannot assign a value to $7 $7 1s used to find the return value of the last executed command Example: touch filet echo $? 0 the return value of touch file should be 0 (default "success" return value) rm filel echo $? 0 $rm filed rm: cannot remove 'file1': No such file or echo $2 1 @ Double ampersand && The shell will interpret && as a logical AND. When using && the second command is executed only if the first one succeeds (returns a zero exit status). Example: echo first && echo second first second zecho first && echo second zecho: command not found Another example of the same logical AND principle. This example starts with a working ed followed by Is, then a non-working ed which is not followed by Is. mkdir gen cd gen touch f1 f2 3 F55 Fab FAB fabe F4 FA fab fab2 cd gen && Is fl 3 FSS fab FAB fabc d by Or, Fatemah Al Aesfor & Dr. Atheel Kadhun 202f2 F4 FA — Fab fab2 cd gen && Is ed: gen: No such file or directory ® Double vertical bar || The || represents a logical OR. The second command is executed only when the first command fails (returns a non-zero exit status). Example: echo first || echo second ; echo third first third Another example of the same logical OR principle: cd gen || Is | ed gen || Is bash: cd: gen: No such file or directory filel file3 FileSS fileab FileAB fileahe file? File4 FileA Pileab fileab2 | © Combining && and || ‘The logical AND and logical OR can be used to write an if-then-else structure on the command line. This example uses echo to display whether the rm command was successful. Example: rm filel && echo It worked! |] echo It failed! Itworked! rm filel && echo It worked! || echo It failed! rm: cannot remove ‘filel':No such file or directory It failed! @ Pound sign # | Everything written after a pound sign (#) is ignored by the shell. This is useful to write a shell comment, but has no influence on the command execution or shell expansion Example: mkdir test #we createa directory cd test ##HH we enter the directory Is # is itempty? © Escaping special characters \ The backslash \ character enables the use of control characters, but without the shell interpreting it, this is called escaping characters. Example: echo hello \; world hello; world yr. Fatemah Al Assfor @ Dr. Atheel Kadnum 202echo hello\ \ \ world hello world echo escaping \\\ \#\ \&\ \"\\rescaping\#&" echo escaping \\\?\*\"\" escaping \?*" @ Shell Variables: The variables are often read by applications. ® Dollar Sign $ An important character interpreted by the shell is the $. The shell will look for an environment variable named like the string following the $ and replace it with the value of the variable (or with nothing if the variable does not exist). These are some examples using SHOSTNAME, SUSER, $UID, $SHELL, and $HOME. Example: echo This is the $SHELL shell ‘This is the /bin/bash shell echo This is $SHELL on computer $HOSTNAME This is /bin/bash on computer mathlab -VGN-CRN320E Example: echo My homedir is SHOME My homedir is /home/mathlab @ $Path The $PATH variable is determines where the shell is looking for commands to execute (unless the command is builtin or aliased). This variable contains a list of directories, separated by colons. Example: echo $PATH /usr/lib/lightdm /lightdm:/ust/local/sbin:/usr/local /bin:.. The shell will not look in the current directory for commands to execute! (Looking for executable files in the current directory provided an easy way to hack PC-DOS computers). If you want the shell to look in the current directory, then add a,’ at the end of your PATH. Example: PATH=$PATH:. echo $PATH /usr lib /lightdm /lightdm:/usr/local/sbin:/usr/local /bin:... pa 5 Exp-1/ Prepared by br. Fatemah Al Assfor & Dr.® Creating Variables Example: ‘This example creates the variable $MyVar and sets its value. It then uses echo to verify the value. Example: MyVar=555 ‘echo $MyVar 555 e Quotes The double quotes " still allow the parsing of variables, whereas single quotes prevent this, Examples: MyVar=555 echo $MyVar 555 echo "$MyVar" 555 echo '$MyVar’ $MyVar Examples: city=Burtonville echo "We are in $city today" We are in Burtonville today echo 'We are in Scity today’ We are in Scity today © Shell Embedding Shells can be embedded on the command line, or in other words, the command line scan can spawn new processes containing a fork of the current shell. You can use variables to prove that new shells are created. The following example shows the variable $var1 only exists in the (temporary) sub shell. echo $var1 echo $(vart= 5 echo $vart vecho $varl) You can embed a shell in an embedded shell, this is called nested embedding of shells. The following example shows an embedded shell inside an embedded shell. 4 by Dr. Fatenah Al Assfor & br. Atheel Kaghum 2023Example: A=shell echo $CSBSA $(B=sub;echo $C$B$A; echo $(C=sub;echo $C$B$A)) shell subshell subsubshell O Exercise: 1- Explain the difference between the following commands: echoHello and echo -n Hello. 2- Display A BC with two spaces between B and C. 3+ Complete the following command (do not use spaces) to display exactly the following output: 44458 10+14=24 4- Use echo to display the following exactly: I\\ 5- When you type passwd, which file is executed? 6- Echo it worked when touch test42 works, and echo it failed when the touch failed. All on one command line as a normal user (not root). Test this line in your home directory and in /bin/ | 7- Write a command line that executes rm file55. Your command line should print ‘success’ if fileSS is removed, and print ‘failed’ if there was a problem, 8- Use echo to display Hello followed by your username. (use a bash variable!) | echo Hello $USER. pe.7 Exp.1/ Prepaced by Dr. Al Assfor & Dr. Atheal Kadhum 2023 |
You might also like
Shell Scripting OS LAB
PDF
No ratings yet
Shell Scripting OS LAB
41 pages
UNIX Shell Scripting Basics
PDF
No ratings yet
UNIX Shell Scripting Basics
200 pages
Shell Scripting
PDF
No ratings yet
Shell Scripting
27 pages
5 CPS393 LinuxOddsEnds
PDF
No ratings yet
5 CPS393 LinuxOddsEnds
59 pages
05 Bash Script Intro
PDF
No ratings yet
05 Bash Script Intro
49 pages
Unit 2
PDF
No ratings yet
Unit 2
83 pages
Bash Scripting Session - 2
PDF
No ratings yet
Bash Scripting Session - 2
52 pages
Shell Scripting Presentation
PDF
No ratings yet
Shell Scripting Presentation
91 pages
UNIX 20shell 20scripting 20V1.0
PDF
No ratings yet
UNIX 20shell 20scripting 20V1.0
178 pages
Bash Cookbook
PDF
No ratings yet
Bash Cookbook
21 pages
06 Shell
PDF
No ratings yet
06 Shell
92 pages
Bash Examples
PDF
No ratings yet
Bash Examples
56 pages
Wa0024.
PDF
No ratings yet
Wa0024.
23 pages
Shell Script
PDF
No ratings yet
Shell Script
49 pages
Linux Shell Scripting
PDF
100% (2)
Linux Shell Scripting
23 pages
Linux CPU
PDF
No ratings yet
Linux CPU
12 pages
04 ShellProgramming
PDF
No ratings yet
04 ShellProgramming
73 pages
Unix Commands
PDF
No ratings yet
Unix Commands
36 pages
Automating System Administrations
PDF
No ratings yet
Automating System Administrations
40 pages
7 How Shell See
PDF
No ratings yet
7 How Shell See
13 pages
Shellscript Programming Using Bash: GUI ATM
PDF
No ratings yet
Shellscript Programming Using Bash: GUI ATM
22 pages
Linux 06
PDF
No ratings yet
Linux 06
51 pages
420-N23 Operating Systems - Topic 14 - Scripting I
PDF
No ratings yet
420-N23 Operating Systems - Topic 14 - Scripting I
38 pages
Bash Data Handling: COMP2101 Winter 2019
PDF
No ratings yet
Bash Data Handling: COMP2101 Winter 2019
25 pages
Book of Spells
PDF
No ratings yet
Book of Spells
6 pages
Unit+3 Linux
PDF
No ratings yet
Unit+3 Linux
91 pages
Unix Shell Scripts: 2.1 Direct Interpretation
PDF
No ratings yet
Unix Shell Scripts: 2.1 Direct Interpretation
8 pages
Prepared by - , Deptt of Information Technology Assam University Silchar
PDF
No ratings yet
Prepared by - , Deptt of Information Technology Assam University Silchar
29 pages
UNIX Shell Scripting Talk
PDF
No ratings yet
UNIX Shell Scripting Talk
52 pages
Starting, Exiting, Reading and Writing Files in Emacs
PDF
No ratings yet
Starting, Exiting, Reading and Writing Files in Emacs
42 pages
Programming Shell Scripts
PDF
No ratings yet
Programming Shell Scripts
26 pages
08 Automation Shell - Scripting
PDF
No ratings yet
08 Automation Shell - Scripting
49 pages
Bash Ref Compressed
PDF
No ratings yet
Bash Ref Compressed
2 pages
Shell Scripting
PDF
No ratings yet
Shell Scripting
109 pages
Intro To Shell Scripting
PDF
No ratings yet
Intro To Shell Scripting
6 pages
2 IT628 Systems Programming-Shell Scripting, Jan 5
PDF
No ratings yet
2 IT628 Systems Programming-Shell Scripting, Jan 5
16 pages
Bourn Shell Ref
PDF
No ratings yet
Bourn Shell Ref
2 pages
Bash 2
PDF
No ratings yet
Bash 2
22 pages
Shell Scripts Language
PDF
No ratings yet
Shell Scripts Language
63 pages
Unix 3 Sem
PDF
No ratings yet
Unix 3 Sem
25 pages
Introduction To Shell Scripting
PDF
No ratings yet
Introduction To Shell Scripting
21 pages
Standard IO and Pipes - Scripts
PDF
100% (1)
Standard IO and Pipes - Scripts
35 pages
Activity 1 The Linux Command Line
PDF
No ratings yet
Activity 1 The Linux Command Line
9 pages
Vijay Scripting Bof
PDF
No ratings yet
Vijay Scripting Bof
29 pages
Shell Scripting: Presented by - Mukesh Halwan Premkanth Mengani
PDF
No ratings yet
Shell Scripting: Presented by - Mukesh Halwan Premkanth Mengani
25 pages
COMP313 Prac2 2019
PDF
No ratings yet
COMP313 Prac2 2019
5 pages
How To Write A Shell Script
PDF
No ratings yet
How To Write A Shell Script
56 pages
Part 1 Linux Chapter 5 Shell Programming
PDF
No ratings yet
Part 1 Linux Chapter 5 Shell Programming
25 pages
Shell Scripts and Grep Command
PDF
No ratings yet
Shell Scripts and Grep Command
14 pages
Last Time : On The Website
PDF
No ratings yet
Last Time : On The Website
63 pages
What Is A Shell-New
PDF
No ratings yet
What Is A Shell-New
15 pages
Unix Shell Scripting Chapter - 1: List Files That Begin With A Lowercase Letter and Don't End With A Digit
PDF
No ratings yet
Unix Shell Scripting Chapter - 1: List Files That Begin With A Lowercase Letter and Don't End With A Digit
10 pages
Shell Scripts and Awk: Tim Love Tpl@eng - Cam.ac - Uk February 26, 2009
PDF
No ratings yet
Shell Scripts and Awk: Tim Love Tpl@eng - Cam.ac - Uk February 26, 2009
16 pages
Linux Shell Programming
PDF
No ratings yet
Linux Shell Programming
32 pages
Introduction To UNIX: Lecture Eight: 8.1 Objectives
PDF
No ratings yet
Introduction To UNIX: Lecture Eight: 8.1 Objectives
9 pages
Shell Scripting Overview
PDF
No ratings yet
Shell Scripting Overview
4 pages