Chapter 2 Introduction to Bash 1 Dr. Hadeel Alazzam Scripting Programming 1
2 • Output • Variables • Input • Conditionals • Looping • Functions • Pattern Matching in Bash • Writing Your First Script • Practical Examples Outline
3 Bash • Bash is more than just a simple command-line interface for running programs. • It is a programming language in its own right. • Its default operation is to launch other programs. • Bash has features to support input and output, and control structures such as if, while, for, case, and more. • Its basic data type is strings (such as filenames and pathnames) but it also supports integers. • It doesn’t directly support floating-point numbers • Other commands can be used for that.
4 Output • Bash has the ability to output information to the screen. • Output can be achieved by using the echo command: • You may also use the printf built-in command, which allows for additional formatting:
Variables • Bash variables begin with an alphabetic character or underscore followed by alphanumeric characters. • By default, they are string variables unless declared otherwise. • To assign a value to the variable, write something like this: • To print out (retrieve) the value by using the echo command —use the $ in front of the variable name, like this: • To assign a series of words to the variable, use quotation marks around the value, • Double quotes will allow other substitutions to occur inside the string. 5 Dr.AryafAl-adwan,AutonomousSystemsDept 5
Variables • You can store the output of a shell command by using $( ) as follows: • This will executes the command pwd in a subshell, and instead of printing the result to stdout, it will store the output of the command in the variable CMDOUT. • Note that you can pipe multiple commands within the $ ( ). 6 Dr.AryafAl-adwan,AutonomousSystemsDept 6
Positional Parameters • You can pass data into the commands by using arguments or parameters. • Each parameter is separated by the space. • Parameters can be accessed inside bash by using a special set of identifiers. 7 Dr.AryafAl-adwan,AutonomousSystemsDept 7 • The first parameter passed into the script can be accessed using $1. • The second using $2, and so on. • $0 is a special parameter that holds the name of the script. • $# returns the total number of parameters.
Input 8 Dr. Aryaf Al-adwan, Autonomous Systems Dept • User input is received in bash by using the read command. • The read command obtains user input from stdin and stores it in a specified variable. • The following script reads user input into the MYVAR variable and then prints it to the screen: • You have already seen (in the previous chapter) how to redirect that input to come from files. 8
Conditionals 9 Dr. Aryaf Al-adwan, Autonomous Systems Dept • Bash has a rich variety of conditionals. • Many, but not all, begin with the keyword if. • Any command or program that you invoke in bash may produce output but it will always return a success or fail value. • This value can be found in the $? variable immediately after a command has run. • A return value of 0 is considered “success” or “true”. • Any nonzero value is considered “error” or “false.” • The simplest form of the if statement uses this fact. It takes the following form: • Note: Using 0 for true and nonzero for false is the exact opposite of many programming languages (C++, Java, Python, …). 9
Conditionals 1 0 Dr. Aryaf Al-adwan, Autonomous Systems Dept • For example, the following script attempts to change directories to /tmp. • If that command is successful (returns 0), the body of the if statement will execute. • Bash can even handle a pipeline of commands in a similar fashion: 10
• With a pipeline, it is the success/failure of the last command in the pipeline that determines if the “true” branch is taken. • This series of commands will be “true” even if no pdf string is found by the grep command. • Because the wc command (a word count of the input) will succeed and print the following: • That output indicates zero lines, zero words, and zero bytes (characters) when no output comes from the grep command. • That is still a successful (thus true) result for wc, not an error or failure. • It counted as many lines as it was given, even if it was given zero lines to count. Conditionals 1 1 Dr. Aryaf Al-adwan, Autonomous Systems Dept 11
• A more typical form of if used for comparison makes use of the compound command [[ or the shell built-in command [ or test. • Use these to test file attributes or to make comparisons of value. • To test whether a file exists on the filesystem: • Table 2-1 lists additional tests that can be done on files by using if comparisons. Conditionals 1 2 Dr. Aryaf Al-adwan, Autonomous Systems Dept 12
• To test whether the variable $VAL is less than the variable $MIN: Conditionals 1 3 Dr. Aryaf Al-adwan, Autonomous Systems Dept 13 • Table 2-2 lists additional numeric tests that can be done using if comparisons.
• If you want to do numerical comparisons with the less-than (<) sign, use the double-parentheses construct “(( ))”. • It assumes that the variables are all numerical and will evaluate them as such. • Empty or unset variables are evaluated as 0. • Inside the parentheses, you don’t need the $ operator to retrieve a value, except for positional parameters like $1 and $2. • so as not to confuse them with the constants 1 and 2. Conditionals 1 4 Dr. Aryaf Al-adwan, Autonomous Systems Dept 14
• In bash, you can even make branching decisions without an explicit if/then construct. • Commands are typically separated by a newline • You can get the same effect by separating them with a semicolon. • If you write cd $DIR ; ls, bash will perform the cd and then the ls. • Two commands can also be separated by either && or || symbols. • If you write cd $DIR && ls ,the ls command will run only if the cd command succeeds. • Similarly, if you write cd $DIR || echo cd failed ,the message will be printed only if the cd fails. • You can use the [[ syntax to make various tests, even without an explicit if: • That means the same as if you had written the following: Conditionals 1 5 Dr. Aryaf Al-adwan, Autonomous Systems Dept 15
• Looping with a while statement is similar to the if construct in that it can take a single command or a pipeline of commands for the decision of true or false. • It can also make use of the brackets or parentheses as in the previous if examples. • In bash, the statements are grouped between two keywords: do and done. Looping 1 6 Dr. Aryaf Al-adwan, Autonomous Systems Dept 16
A for loop is also available in bash, in three variations” 1. Simple numerical looping can be done using the double-parentheses construct. • It looks much like the for loop in C or Java, but with double parentheses and with do and done instead of braces: 2. use for loop to iterate through all the parameters that are passed to a shell script (or function within the script) — that is, $1, $2, $3, and so on. for Loop 1 7 Dr. Aryaf Al-adwan, Autonomous Systems Dept 17
3. Finally, for an arbitrary list of values, use a similar form of the for statement and simply name each of the values you want for each iteration of the loop. • That list can be explicitly written out, like this: • The values used in the for loop can also be generated by calling other programs or using other shell features: for Loop 1 8 Dr. Aryaf Al-adwan, Autonomous Systems Dept 18
• You define a function with syntax like this: • There are a few important considerations to keep in mind with bash functions: • Unless declared with the local built-in command inside the function, variables are global in scope. • The braces are the most commonly used grouping for the function body, but any of the shell’s compound command syntax is allowed. • Redirecting input/output (I/O) on the braces does so for all the statements inside the function. • No parameters are declared in the function definition. Whatever and however many arguments are supplied on the invocation of the function are passed to it. • The function is called (invoked) just as any command is called in the shell. Having defined myfun as a function, you can call it like this: Functions 1 9 Dr. Aryaf Al-adwan, Autonomous Systems Dept 19
• Inside the function definition, arguments are referred to in the same way as parameters to the shell script — as $1, $2, etc. • This means that they “hide” the parameters originally passed to the script. • If you want access to the script’s first parameter, you need to store $1 into a variable before you call the function. • Other variables are set accordingly too. • $# gives the number of arguments passed to the function, whereas normally it gives the number of arguments passed to the script itself. • The one exception to this is $0, which doesn’t change in the function. • It retains its value as the name of the script, (and not of the function). Function Arguments 2 0 Dr. Aryaf Al-adwan, Autonomous Systems Dept 20
• Functions, like commands, should return a status — a 0 if all goes well, and a nonzero value if an error has occurred. • To return other kinds of values (e.g. pathnames or computed values), you can set a variable to hold that value, because those variables are global unless declared local within the function. • Alternatively, you can send the result to stdout; that is, print the answer Returning Value 2 1 Dr. Aryaf Al-adwan, Autonomous Systems Dept 21
• When you need to name a lot of files on a command line, you don’t need to type each and every name. • Bash provides pattern matching (sometimes called wildcarding) to allow you to specify a set of files with a pattern. 1. The easiest wildcard is simply an asterisk (*) or star, which will match any number of any character. • For example, *.txt matches all the files in the current directory that end with the four characters .txt. • When used by itself, therefore, it matches all files in the current directory. • The pattern /usr/bin/g* will match all the files in /usr/bin that begin with the letter g. 2. The question mark (?), which matches a single character. • For example, source.? will match source.c or source.o but not source.py or source.cpp. Pattern Matching in Bash 2 2 Dr. Aryaf Al-adwan, Autonomous Systems Dept 22
3. The square brackets: [ ]. • A match can be made with any one of the characters listed inside the square brackets, so the pattern x[abc]y matches any or all of the files named xay, xby, or xcy, assuming they exist. • You can specify a range within the square brackets, like [0–9] for all digits. • If the first character within the brackets is either an exclamation point (!) or a carat (^), then the pattern means anything other than the remaining characters in the brackets. • For example, [aeiou] would match a vowel, whereas [^aeiou] would match any character (including digits and punctuation characters) except the vowels. Pattern Matching in Bash 2 3 Dr. Aryaf Al-adwan, Autonomous Systems Dept 23
Pattern Matching in Bash 2 4 Dr. Aryaf Al-adwan, Autonomous Systems Dept 24 • Similar to ranges, you can specify character classes within braces. • Table 2-3 lists the character classes and their descriptions. • Character classes are specified like [:ctrl:] but within square brackets (so you have two sets of brackets). • For example, the pattern *[[:punct:]]jpg will match any filename that has any number of any characters followed by a punctuation character, followed by the letters jpg. • So it would match files named wow!jpg or some,jpg or photo.jpg but not a file named this.is.myjpg, because there is no punctuation character right before the jpg.
Shell Pattern Matching Notes 2 5 Dr. Aryaf Al-adwan, Autonomous Systems Dept 25 • Patterns aren’t regular expressions. • Patterns are matched against files in the filesystem; if the pattern begins with a pathname (e.g., /usr/lib ), the matching will be done against files in that directory. • If no pattern is matched, the shell will use the special pattern-matching characters as literal characters of the filename. • For example, if your script indicates echo data > /tmp/*.out, but there is no file in /tmp that ends in .out, then the shell will create a file called *.out in the /tmp directory. • Remove it like this: rm /tmp/*.out by using the backslash to tell the shell not to pattern-match with the asterisk. • No pattern matching occurs inside quotes (either double or single quotes), so if your script says echo data > "/tmp/*.out",it will create a file called /tmp/*.out.
Writing Your First Script — Detecting Operating System Type 2 6 Dr. Aryaf Al-adwan, Autonomous Systems Dept 26 • We use the type built-in in bash to tell us what kind of command (alias, keyword, function, built-in, or file) its arguments are. • The -t option tells it to print nothing if the command isn’t found. • The command returns as “false” in that case. • We redirect all the output (both stdout and stderr) to /dev/null, thereby throwing it away, as we want to know only whether the wevtutil command was found.
Lab Exercises 2 7 Dr. Aryaf Al-adwan, Autonomous Systems Dept 27
End 28 Dr. Aryaf Al-adwan, Autonomous Systems Dept 28

Chapter 2: Introduction to Bash Scripting

  • 1.
    Chapter 2 Introduction toBash 1 Dr. Hadeel Alazzam Scripting Programming 1
  • 2.
    2 • Output • Variables •Input • Conditionals • Looping • Functions • Pattern Matching in Bash • Writing Your First Script • Practical Examples Outline
  • 3.
    3 Bash • Bash ismore than just a simple command-line interface for running programs. • It is a programming language in its own right. • Its default operation is to launch other programs. • Bash has features to support input and output, and control structures such as if, while, for, case, and more. • Its basic data type is strings (such as filenames and pathnames) but it also supports integers. • It doesn’t directly support floating-point numbers • Other commands can be used for that.
  • 4.
    4 Output • Bash hasthe ability to output information to the screen. • Output can be achieved by using the echo command: • You may also use the printf built-in command, which allows for additional formatting:
  • 5.
    Variables • Bash variablesbegin with an alphabetic character or underscore followed by alphanumeric characters. • By default, they are string variables unless declared otherwise. • To assign a value to the variable, write something like this: • To print out (retrieve) the value by using the echo command —use the $ in front of the variable name, like this: • To assign a series of words to the variable, use quotation marks around the value, • Double quotes will allow other substitutions to occur inside the string. 5 Dr.AryafAl-adwan,AutonomousSystemsDept 5
  • 6.
    Variables • You canstore the output of a shell command by using $( ) as follows: • This will executes the command pwd in a subshell, and instead of printing the result to stdout, it will store the output of the command in the variable CMDOUT. • Note that you can pipe multiple commands within the $ ( ). 6 Dr.AryafAl-adwan,AutonomousSystemsDept 6
  • 7.
    Positional Parameters • Youcan pass data into the commands by using arguments or parameters. • Each parameter is separated by the space. • Parameters can be accessed inside bash by using a special set of identifiers. 7 Dr.AryafAl-adwan,AutonomousSystemsDept 7 • The first parameter passed into the script can be accessed using $1. • The second using $2, and so on. • $0 is a special parameter that holds the name of the script. • $# returns the total number of parameters.
  • 8.
    Input 8 Dr. Aryaf Al-adwan,Autonomous Systems Dept • User input is received in bash by using the read command. • The read command obtains user input from stdin and stores it in a specified variable. • The following script reads user input into the MYVAR variable and then prints it to the screen: • You have already seen (in the previous chapter) how to redirect that input to come from files. 8
  • 9.
    Conditionals 9 Dr. Aryaf Al-adwan,Autonomous Systems Dept • Bash has a rich variety of conditionals. • Many, but not all, begin with the keyword if. • Any command or program that you invoke in bash may produce output but it will always return a success or fail value. • This value can be found in the $? variable immediately after a command has run. • A return value of 0 is considered “success” or “true”. • Any nonzero value is considered “error” or “false.” • The simplest form of the if statement uses this fact. It takes the following form: • Note: Using 0 for true and nonzero for false is the exact opposite of many programming languages (C++, Java, Python, …). 9
  • 10.
    Conditionals 1 0 Dr. Aryaf Al-adwan,Autonomous Systems Dept • For example, the following script attempts to change directories to /tmp. • If that command is successful (returns 0), the body of the if statement will execute. • Bash can even handle a pipeline of commands in a similar fashion: 10
  • 11.
    • With apipeline, it is the success/failure of the last command in the pipeline that determines if the “true” branch is taken. • This series of commands will be “true” even if no pdf string is found by the grep command. • Because the wc command (a word count of the input) will succeed and print the following: • That output indicates zero lines, zero words, and zero bytes (characters) when no output comes from the grep command. • That is still a successful (thus true) result for wc, not an error or failure. • It counted as many lines as it was given, even if it was given zero lines to count. Conditionals 1 1 Dr. Aryaf Al-adwan, Autonomous Systems Dept 11
  • 12.
    • A moretypical form of if used for comparison makes use of the compound command [[ or the shell built-in command [ or test. • Use these to test file attributes or to make comparisons of value. • To test whether a file exists on the filesystem: • Table 2-1 lists additional tests that can be done on files by using if comparisons. Conditionals 1 2 Dr. Aryaf Al-adwan, Autonomous Systems Dept 12
  • 13.
    • To testwhether the variable $VAL is less than the variable $MIN: Conditionals 1 3 Dr. Aryaf Al-adwan, Autonomous Systems Dept 13 • Table 2-2 lists additional numeric tests that can be done using if comparisons.
  • 14.
    • If youwant to do numerical comparisons with the less-than (<) sign, use the double-parentheses construct “(( ))”. • It assumes that the variables are all numerical and will evaluate them as such. • Empty or unset variables are evaluated as 0. • Inside the parentheses, you don’t need the $ operator to retrieve a value, except for positional parameters like $1 and $2. • so as not to confuse them with the constants 1 and 2. Conditionals 1 4 Dr. Aryaf Al-adwan, Autonomous Systems Dept 14
  • 15.
    • In bash,you can even make branching decisions without an explicit if/then construct. • Commands are typically separated by a newline • You can get the same effect by separating them with a semicolon. • If you write cd $DIR ; ls, bash will perform the cd and then the ls. • Two commands can also be separated by either && or || symbols. • If you write cd $DIR && ls ,the ls command will run only if the cd command succeeds. • Similarly, if you write cd $DIR || echo cd failed ,the message will be printed only if the cd fails. • You can use the [[ syntax to make various tests, even without an explicit if: • That means the same as if you had written the following: Conditionals 1 5 Dr. Aryaf Al-adwan, Autonomous Systems Dept 15
  • 16.
    • Looping witha while statement is similar to the if construct in that it can take a single command or a pipeline of commands for the decision of true or false. • It can also make use of the brackets or parentheses as in the previous if examples. • In bash, the statements are grouped between two keywords: do and done. Looping 1 6 Dr. Aryaf Al-adwan, Autonomous Systems Dept 16
  • 17.
    A for loopis also available in bash, in three variations” 1. Simple numerical looping can be done using the double-parentheses construct. • It looks much like the for loop in C or Java, but with double parentheses and with do and done instead of braces: 2. use for loop to iterate through all the parameters that are passed to a shell script (or function within the script) — that is, $1, $2, $3, and so on. for Loop 1 7 Dr. Aryaf Al-adwan, Autonomous Systems Dept 17
  • 18.
    3. Finally, foran arbitrary list of values, use a similar form of the for statement and simply name each of the values you want for each iteration of the loop. • That list can be explicitly written out, like this: • The values used in the for loop can also be generated by calling other programs or using other shell features: for Loop 1 8 Dr. Aryaf Al-adwan, Autonomous Systems Dept 18
  • 19.
    • You definea function with syntax like this: • There are a few important considerations to keep in mind with bash functions: • Unless declared with the local built-in command inside the function, variables are global in scope. • The braces are the most commonly used grouping for the function body, but any of the shell’s compound command syntax is allowed. • Redirecting input/output (I/O) on the braces does so for all the statements inside the function. • No parameters are declared in the function definition. Whatever and however many arguments are supplied on the invocation of the function are passed to it. • The function is called (invoked) just as any command is called in the shell. Having defined myfun as a function, you can call it like this: Functions 1 9 Dr. Aryaf Al-adwan, Autonomous Systems Dept 19
  • 20.
    • Inside thefunction definition, arguments are referred to in the same way as parameters to the shell script — as $1, $2, etc. • This means that they “hide” the parameters originally passed to the script. • If you want access to the script’s first parameter, you need to store $1 into a variable before you call the function. • Other variables are set accordingly too. • $# gives the number of arguments passed to the function, whereas normally it gives the number of arguments passed to the script itself. • The one exception to this is $0, which doesn’t change in the function. • It retains its value as the name of the script, (and not of the function). Function Arguments 2 0 Dr. Aryaf Al-adwan, Autonomous Systems Dept 20
  • 21.
    • Functions, likecommands, should return a status — a 0 if all goes well, and a nonzero value if an error has occurred. • To return other kinds of values (e.g. pathnames or computed values), you can set a variable to hold that value, because those variables are global unless declared local within the function. • Alternatively, you can send the result to stdout; that is, print the answer Returning Value 2 1 Dr. Aryaf Al-adwan, Autonomous Systems Dept 21
  • 22.
    • When youneed to name a lot of files on a command line, you don’t need to type each and every name. • Bash provides pattern matching (sometimes called wildcarding) to allow you to specify a set of files with a pattern. 1. The easiest wildcard is simply an asterisk (*) or star, which will match any number of any character. • For example, *.txt matches all the files in the current directory that end with the four characters .txt. • When used by itself, therefore, it matches all files in the current directory. • The pattern /usr/bin/g* will match all the files in /usr/bin that begin with the letter g. 2. The question mark (?), which matches a single character. • For example, source.? will match source.c or source.o but not source.py or source.cpp. Pattern Matching in Bash 2 2 Dr. Aryaf Al-adwan, Autonomous Systems Dept 22
  • 23.
    3. The squarebrackets: [ ]. • A match can be made with any one of the characters listed inside the square brackets, so the pattern x[abc]y matches any or all of the files named xay, xby, or xcy, assuming they exist. • You can specify a range within the square brackets, like [0–9] for all digits. • If the first character within the brackets is either an exclamation point (!) or a carat (^), then the pattern means anything other than the remaining characters in the brackets. • For example, [aeiou] would match a vowel, whereas [^aeiou] would match any character (including digits and punctuation characters) except the vowels. Pattern Matching in Bash 2 3 Dr. Aryaf Al-adwan, Autonomous Systems Dept 23
  • 24.
    Pattern Matching inBash 2 4 Dr. Aryaf Al-adwan, Autonomous Systems Dept 24 • Similar to ranges, you can specify character classes within braces. • Table 2-3 lists the character classes and their descriptions. • Character classes are specified like [:ctrl:] but within square brackets (so you have two sets of brackets). • For example, the pattern *[[:punct:]]jpg will match any filename that has any number of any characters followed by a punctuation character, followed by the letters jpg. • So it would match files named wow!jpg or some,jpg or photo.jpg but not a file named this.is.myjpg, because there is no punctuation character right before the jpg.
  • 25.
    Shell Pattern MatchingNotes 2 5 Dr. Aryaf Al-adwan, Autonomous Systems Dept 25 • Patterns aren’t regular expressions. • Patterns are matched against files in the filesystem; if the pattern begins with a pathname (e.g., /usr/lib ), the matching will be done against files in that directory. • If no pattern is matched, the shell will use the special pattern-matching characters as literal characters of the filename. • For example, if your script indicates echo data > /tmp/*.out, but there is no file in /tmp that ends in .out, then the shell will create a file called *.out in the /tmp directory. • Remove it like this: rm /tmp/*.out by using the backslash to tell the shell not to pattern-match with the asterisk. • No pattern matching occurs inside quotes (either double or single quotes), so if your script says echo data > "/tmp/*.out",it will create a file called /tmp/*.out.
  • 26.
    Writing Your FirstScript — Detecting Operating System Type 2 6 Dr. Aryaf Al-adwan, Autonomous Systems Dept 26 • We use the type built-in in bash to tell us what kind of command (alias, keyword, function, built-in, or file) its arguments are. • The -t option tells it to print nothing if the command isn’t found. • The command returns as “false” in that case. • We redirect all the output (both stdout and stderr) to /dev/null, thereby throwing it away, as we want to know only whether the wevtutil command was found.
  • 27.
    Lab Exercises 2 7 Dr. AryafAl-adwan, Autonomous Systems Dept 27
  • 28.
    End 28 Dr. Aryaf Al-adwan,Autonomous Systems Dept 28

Editor's Notes

  • #17  -n: This option is used for omitting echoing trailing new lines. -q  silent (quite) without print ((i=i+1))