0% found this document useful (0 votes)
13 views10 pages

4 Shell Scripting - Notes

Lecture 3 covers the fundamentals of shell scripting, specifically using Bash. It introduces key concepts such as writing scripts, using variables, control flow directives, and functions, as well as the importance of shebangs and exit codes. The lecture includes practical exercises to reinforce learning and understanding of Bash scripting mechanics.
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
0% found this document useful (0 votes)
13 views10 pages

4 Shell Scripting - Notes

Lecture 3 covers the fundamentals of shell scripting, specifically using Bash. It introduces key concepts such as writing scripts, using variables, control flow directives, and functions, as well as the importance of shebangs and exit codes. The lecture includes practical exercises to reinforce learning and understanding of Bash scripting mechanics.
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
You are on page 1/ 10
CS45, Lecture 3: Shell Scripting Contents 1 Lecture Overview 2 What is Shell Seripting? 3. Bash Scripting: Basic Mechanics 3.1, Your Very First Script 32 Shebangs Running a Script, 4 Bash Scripti 4.1 Variables 42 Strings Variables and Strings 5 Bash Scripti 5.1 If Statements 5.2 While Loops Control Flow Directives 6 Bash Scripting: Arguments and Functions 6.1 Arguments 62 Functions . 63. Exercise 2 7 Exit Codes and Command Substitution 7.1 Bxit Codes 72 Exercise 3. a 7.3 Command Substitution 8 Bash Scripting: Other Syntax 8.1 Comparisons 82 Exercise 4. 1 Lecture Overview In Lecture 3, we learned how to use shell commands and pipelines to manipulate and analyze data. We also earned how to write regular expressions and how to incorporate these into tools such as eed. Finally, we earned how to run complex shell commands such as grep, sort, uniq, and xargs. In today’s lecture we will learn how to write shell scripts and the syntax of shell scripts. 2 What is Shell Scripting? We've already seen how to execute simple commands in the shell and pipe multiple commands together. Sometimes, we want to run many commands together and make use of control flow expressions such as conditionals and loops. This is where shell seripting comes in, A shell script is a text file that contains a sequence of commands for a Unix-based operating system. Tt is called a seript because it combines a sequence of commands-that would otherwise have to be typed into a keyboard one at a time-into a single seript. ‘Most shells have their own scripting language, each with variables, control flow, and its own syntax. (You ‘may have heard of bash seripting as a popular seripting language. Bash scripting i a type of shell seripting:) What makes shell scripting different from other scripting languages is that it is optimized for perforn shell-related tasks. Creating command pipelines, saving results into fle, and reading from standard input are primitives in shell sripting, making it easier to use compared to other seripting languages. (For example, ifyou want to run the ed command in Python, you would need to import the os library and then call ebase from that library.) 3 Bash Scripting: Basic Mechanics Bash seripting refers to writing a script for a bash shell (Bourne Again SHell). You can cheek what shell you are using by running pe -p $8. If you ate on Linux, your default shell should be a bash shell. If you are ‘on macOS or Windows, you may need to switch toa bash shell. On macOS run exee bash to launch a bash shell, On Windows, run bash to launch a bash shell, 3.1 Your Very First Script Now that we have a bash shell, we can write our very frst bash script, called netio.2h. Shell scripts normally: end with the en ending to indicate that they are scripts. To run a script, you will type the following at your command line: sh netie.2h ‘This script will make use of the ecko command that we learned about in Lecture 2 fi/usr/bin/eny bash cho "Hello vorla 3.2 Shebangs ‘The first line in our script (#1/asr/ene/ bash ) is called a shebang, or sharp exclamation. It the combination pound symbol (#é) and an exclamation mark (!). The shebang is used to specify the interpreter that ven seript will be run with. In our ease, we indicate that we want our script to be run with a bash interpreter (i.e. a bash shell). If you want. to run your seript with a zsh shell, you would change your shebang. to reflect as such. ‘There are a number of different ways to write your shebang stch as #1/ase/esa/eny bash ancl #1/bin/bash. We recommend that. you always use the former as it inereases the portability of your script. The env command tells the system to resolve the bash command wherever it lives in the system, as opposed to just lookin inside of /oin Another example of a shebang that may be useful to many of you is using a shebang to write a python seript, You may be familiar with writing a Python script and then running the script by calling pyekoas. Yous can also create a Python script and specify that it should be run with Python using the shebang. 3.3 Running a Script ‘You can always run a shell script by simply prepending it with a shell interpreter program such as sh he1io.8 ‘bash hello,sh OF 20h hello. sh You can also run a seript by turning it into an executable program and then running it. First, you need to ‘turn the program into an executable using the etea (change mode) command. This command is used for changing the file permissions on a file, such as making the file executable. Tn our ease, we will ehnod with the +x argument to turn the script into an executable. You can do so as follows: hod + hello. ‘To run the seript, you would then simply run the program with ./setto.eh (which is likely similar to how you have run other programs in the past. 4 Bash Scriptiny 4.1 Variables Variables and Strings Now that we have a basic script, let's talk about the mechanics of bash seripting. When writing a bash script, you can assign variables using the syntax xefoo. You can then access this variable using the syntax $e. One thing to be careful of is that when you assign a variable in a bash script, you should not add extra spaces. If you write x = feo then the line will be interpreted as running a program called x with the arguments = and foo. In general, shell scripts interpret the space character as an argument splitter. 4.2. Strings We can also define strings in a bash seript. If we want to define a string literal, we will use single quotation marks: #27. If we want to define a string that allows substitution, we will use double quotes: *e"- The double quotes will allow for substitution of variables: echo *82 # prints $x cho "Sx" Pipette 00 5 Bash Scripting: Control Flow Directives Like other programming languages, bash scripts also have control flow directives such as 4, for, white, and case 5.1 If Statements ‘The syntax for writing an if statement in bash is as follows: Ai/usr/bin/ony bash se ¢ conprtrox 1 then do sonething ‘The condition we are interested in is denoted by comprrzox Lot's take a look at an example of a bash seript with an if statement: A i/usr/bin/eny bash it C faun -gr 100 1 then ‘echo “That ’s a big nunber!* In this seript, we begin by assigning a variable aux to be equal to 101 on line 3. We then check whether sux is greater than 100. Notice that we use the syntax saua to access the value of sux. We use the comparison operator -ge to compare num to 100. In bash, comparisons for integers and strings are done differently. ~ge is an integer comparison operator while > is a string comparison operator (that compares ASCII values). ‘The use of the square brackets is actually a synonym for the test command, which tests the validity of a command or statement. We can also write an if-statement with multiple conditions. In this example, we will check if mum is both areater than 100 and less than 1000. #t/usr/bin/env bash nun=101 fe $nuz gt 100] ae ¢ fnum 18 1000 7 ‘echo “That's a big (but not a too big) number!” Again, in this seript we assign mux to be 101. We first check if mux is greater than 190. We than add a second condition using the ag syntax. Our second condition checks if aux is less than 1000. We can also use 13 (for else if) and eise if we have multiple different blocks of code. Here is the syntax for using s#, e1sf, and e1se Ai/usr/bin/ony bash ae ( cowprtrox 1 then # do soncthing '#( compatrow 3 4 do sonething else oise # do sonething totally different ‘The above syntax should look fairly similar to the traditional af statement. Here is an example of code that uses 4f, a14f, and el fi/use/bin/env bash pun=101 ras ‘echo “That ’e @ huge number!” ent [ Saum -gt 100) © then: ‘echo “That’s a small number." 5.2 While Loops ‘We can also add while loops to our bash scripts. The syntax for adding a while loop to a bash script is the following: Ai/usr/bin/ony bash waite [ conprtrox 1 ao '# do sonething tone Again, the condition of interest is denoted as coxorrran Here is an example of a script that initials 9 sen to 0 and continues looping until maz reaches a value of Ai/uer/bin/ony bash hile ¢ fun ao ‘echo $num une $ (Causes) ) at 1001 Notice how we access the variable mum with the $ syntax: $mum. We use the -1¢ flag to compare mum to 400. When then print the value of mum using the ecko command. ‘To increment the value of nam, we use the double parentheses (C.. ) for arithmetic evaluation. Inside of the double parentheses, we ean increment the value of um by 1 5.3. For Loops To declare a for loop in bash, we can declare either an index-based for loop or a range-based for-loop. To declare an index based for-loop, we will use the following syntax: Ht/use/bin/env bash for VARIABLE in 1.2.3 .. 8 # do sonething done Perhaps, we are interested in implementing the above kite loop as for loop: fi/usr/bin/ony bash for 4 sm (0..99) a0 ‘echo $num une $ (Cunt) ) Notice that we define our iterator 4 and we set the bounds of the for-loop to be 0 and 99, 5.4 Exercise 1 Lot's put our scripting expertise to use and write a bash script. You should write a script called san toop.h that loops through every number 4 through 20 and prints each number to standard output. ‘The seript should also conditionally print 1% big! for every number larger than 10 Solut At/usr/bin/eny bash ere is one possible solution : sume for 4 4m (1.20) ‘echo $1 ($i -gr 101 echo "I'm big “4 done 6 Bash Scripting: Arguments and Functions 6.1 Arguments Our bigaun.sh script isn't very interesting as it will always print the same thing: "That's big mumber!” Let's take a look at how we might use command line arguments to make this script a little more interesting. In bash, the variables $1 - $9 refers to the arguments to a script. The variable 0 refers to the name of the script. For example, consider the following: ‘adrazendehinking-cosputer C845 % sh ay-script.sh a In this case, the name of the script (ny-serspt.eh) is defined by the variable $0. The first argument to the script (aysiet) is defined by the variable $1. In the case of our big:mamber.sh script, we can change the value fof mun to be dependent on the first argument that is passed in when the script is invoked. In other words, ‘nun will simply be the value of $1 Let's assume that we invoke the seript as follows: ‘adrazentayelet-computer C846 J oh big-aun.sh 102 In our script, we can replace the line musei01 to be mane fi/usr/bin/env bash nuns Sf C $num -gt 100 1 ‘echo “That's a big number!" 1 By changing line 3, we now set the value of mum to be the first argument from the command line when invoking the script. This means the use could pass a different value every time they invoke the seript. 6.2 Functions ‘We can also define functions in bash, Let's define a function for making a new ditectory and then entering that directory. (This isa pretty commaon thing that users want to do and there is actually a command called acd in UNIX to allow users to do that.) We can use the commands akasr and ed to achieve this. When running axdir, we want to make sure that we also create any necessary intermediate directories, Therefore, ‘wo will want to make sure we pass the -p flag when calling, skate, which creates all the intermediate direc- tories on the path to the final directory that do not already exist When defining a fanetion in bash, you may wonder how to pass arguments to the fimetion. Let's take a look at our function so far and how you might think of passing arguments: fi/uer/bin/eny bash make_and_onter (4irectory_n akdir =p directory_nane cd directory. > Unfortunately, this won’t work in bash. In bash, we will refer to arguments that are passed into a funetion based on their position. For instance, we might use the value of the very first argument and refer to it using a. #i/usr/bin/eav bash make_andonter() akdir =p "Si" ed "ei" > ‘When we want to invoke our function, we will use the following line: In this case, we are calling our function (i.e.make.and.enter) with the argument new-foléer. Our seript will Took as follows: #i/usr/bin/env bash sake andenter() ¢ nkdir

sake my folder $1 $2 7 Exit Codes and Command Substitution 7.1 Exit Codes ‘The notion of exit codes allows for verifying the success or failure of a previous command. An exit code or return value is the way scripts or commands can communicate with each other about how execution went. A return value of 0 means that everything went OK, A return value other than 0 means that an error occurred. $7 provides the return value from the previous command. (For students who are familiar with C/C++, you may notice that the metn@ function always returns an int and that this sae is often 0. The ant returned by ainO is the exit code for the program.) Ifyou ever need a placeholder for a command that succeeds or fails, you can use the true and false commands. true is a command that does nothing except return an exit status of 0. ta1ze is a command that does nothing except return an exit status of 1 Below is an example of a script that randomly generates either 0 or 1 and runs a either the trae or the ¢s2e¢ command based on this random value, The script then checks the return value of the previous command. As you can tell, this script is a bit contrived but it demonstrates how you might use the return value of the previous command as an input to the next command. Ai/usr/bin/eny bash reeure=§(($RANDON % 2)) Af [ Sreault eq 0 then echo "$7" eue fa ‘echo $7 Return values are useful if you want to conditionally execute commands based on the execution of the previous command. In addition to using ifstatements, we can also conditionally execute commands using ae and 11 7.2 Exercise 3 Exercise 3: Write a shell script called 410.checker. at for checking if sl is greater than s2 by lexicographical order © “tat for checking if si has a length greater than 0 © 2.01 for checking if s1 has a length of 0 8.2 Exercise 4 Exercise 4: Write a shell script called tinely-greeting.st that greets you based on the current time. The script should call the date command, extract the current hour (look into using %H) and then print the following greeting based on the time «© If it is between 5AM (05:00) and 12PM (12:00): Good morning! «© IF it is between 12PM (12:00) and 6PM (18:00): Good afternoon! «© If it is betwoen GPM (18:00) and 5AM (5:00): Good night! Solution Here is one possible solution: 10

You might also like