02Lecture-LinuxScripting.pdf

Full Transcript

1901 Lecture Two: Linux Scripting (BASH) Overview    Steve Bourne wrote the Bourne shell (abbreviated sh) which appeared in the Seventh Edition Bell Labs Research version of Unix (1979). The Bourne Again shell (abbreviated bash) is Linux’s version of the Bourne shell that has been updated and fre...

1901 Lecture Two: Linux Scripting (BASH) Overview    Steve Bourne wrote the Bourne shell (abbreviated sh) which appeared in the Seventh Edition Bell Labs Research version of Unix (1979). The Bourne Again shell (abbreviated bash) is Linux’s version of the Bourne shell that has been updated and freely released and is the default shell for most Linux distributions. Other shells include the Korn Shell (ksh), the C Shell (csh), and variations such as tcsh. BASH Scripting Basics      Using vi (or any other text editor), open up either an empty file or an existing shell script. o Normally, shell scripts end in.sh – but it is not required The first line of the text file (shell scripts are text files) must indicate the name of the shell that should be used to execute the shell script. o #!/bin/bash – used to open a new bash shell to execute the script o # is used for comments – but #! is a special directive to the Linux shell Enter your code. o Line comments start with # Save the file. Change the file permissions for the user, group, and other categories so the shell script can be executed: o chmod ugo+x filename or chmod 755 filename o chmod (change mode) modifies the file permissions of the given file The following output can be seen when doing a ls -l command: - rwx rwx rwx 1 auser users 181 Aug 13 22:43 A_File | | | | 1 2 3 4 Each column of information relates to a different characteristic of the file. 1. 2. 3. 4. o Type of file (- = normal file, d = directory, l = symbolic link or shortcut). The user's (owner's) file permissions (r = read, w = write, x = execute). The group's file permissions. All other users' file permissions. chmod ugo+x specifies that execute rights (x) should be added to the user (u), the user’s group (g), and all other users (o) o   chmod 755 is the base 10 number of the binary number 111 101 101  7 (all 1s) turns on the read, write, and execute permissions  5 turns on only the read and execute permission (zero is off – so write is not set).  The file permissions (after the chmod 755 command) will be rwx r-x r-x for the file. To execute the shell script, enter the name of the file (and the path to the file) on the command line. Note: you must specify the path where the shell script is located since the scripts you will be creating will probably reside in a directory that is not listed in the PATH environment variable. (Example:./filename if the file resides in your current working directory.) Commands work exactly the same on the command line as in a script. Variables          Variables are not declared, you just pick a variable name and assign it a value. o myNumber=1 o myString=”World” o Note that you cannot use spaces around the assignment operator! o All variables are assigned as strings but they will be converted to numbers when an appropriate operation is performed – like addition. To access the value of a variable, you use a $ sign To output the value of a variable, you can use the keyword echo o echo $myNumber o echo “Hello $myString” To input a value, you can use the keyword read o read userInput (normally, you would first use echo to let the user know what you are expecting) o input comes in as a string o read val1 val2 val3 will read 3 items from the command line – it is just one read so all three values will have to be entered on the same line. o read -p “Username: “ username will allow for a prompt (-p) and then read o read -ps “Password: “ password will allow for a prompt and then a silent (-s) read – where it won’t echo the user’s input to the screen Bash scripts have access to command line arguments – positional variables. Here are a few… o $0 – the Linux command o $1 – the first command line argument o $2 – the second command line argument o $@ – all arguments at the command line o $# – the number of arguments at the command line Bash scripts have access to environment variables. Here are a few… o $USER – the username of the user running the script o $HOSTNAME – the hostname of the machine the script is running on. o $SECONDS – the number of seconds since the script was started. o $RANDOM – returns a different random number (between 0 - 32767 ) each time it is used. o $LINENO – returns the current line number in the Bash script. You can store the output of a command (or program) in a variable by placing the command in round brackets preceded by the $ sign (this is sometimes called command substitution) … o myVar=$( ls /etc ) o Note that if the output of a command is more than one line, the output is converted to a single line and stored in the variable. Variable Scope o Since scripts will run in their own bash shell, variables declared in a script will only exist in that instance of the bash shell by default. o If you want other scripts (and other bash shells) to be able to access a variable, use the export keyword.  export myVar will make a copy of myVar available in other shell instances – note that a copy and not the original value is exported … so any changes to the copy will not show up in the original. Arithmetic o The basic arithmetic operators are +, -, *, /, % (modulus – remainder from a division), ++, and -o Note that bash works with whole numbers – so 2 / 10 will give you 0 and not 0.2 (and it doesn’t round off, it truncates) o You can use the keyword let  let a=5+4 written with no spaces  let “a = 5 + 4” by using quotes, you can use spaces    let a++ will increment by one  let “a = $1 + 10” will add ten to the first command line argument o You can use double round brackets and a $ sign to do arithmetic (this is the more common way) …  a=$(( 5 + 4 )) will add the two numbers – you can even leave out the spaces (note that there can be no spaces around the assignment operator)  ((a++)) will increment by one – the $ sign is used when using the assignment operator  ((a += 3)) will add 3 to a and assign the result back into a  a=$(( $1 + 10 )) will add ten to the first command line argument To find the length of a variable (the number of characters) use ${#variableName} with no spaces To do string concatenation – write the two variables next to each other o a=$1$2 will concatenate the first argument to the second argument o a=“$1 $2” will take both arguments and put a space in between #!/bin/bash # Ask the user for login details # To execute:./scriptName.sh read -p “Username: ” uservar read -sp “Password: ” passvar echo # this will give us a blank line echo “Thank you $uservar we now have your login details” if-else Logic  Basic if structure: o o o o if [ condition ] then statements fi Note that the condition is inside square brackets and the then keyword is on its own line. The if ends with a fi (which is if spelled backwards). There must be spaces around the square brackets and any comparison operators. Note that multiple lines of statements do not use curly brackets. The square brackets are an alias to the keyword test (though square brackets are more commonly used) …so you could write: if test condition then statements fi  Nested if structure: o if [ condition ] then statements (optional) if [ condition ] then statements fi fi Note that the second if is part of the first if. The second if is terminated first with fi, then the first if is terminated with fi.  if-else structure: if [ condition ] then statements else statements fi  if-elif-else structure: if [ condition ] then statements elif [ condition ] then statements else statements fi  Comparison operators (or test conditions) o To compare strings:  “str1” = “str2” true, if the strings are equal (== can be used in some scripting languages)  “str1” != “str2” true, if the strings are not equal  “str1” \> “str2” true, if the first string has a higher ASCII value than the second string  “str1” \< “str2” true, if the first string has a lower ASCII value than the second string Note: Using the < or > symbols require an escape sequence since they are normally redirection operators.   -n “string” -z “string” true, if the string is not empty (length of string is not zero) true, if the string is empty (length of string is zero) Note: When comparing strings, put both operands (strings in this case) in double quotes (for both string variables and literal strings) in case they contain spaces or one of the variables don’t exist. o o To compare integers:  int1 -eq int2 true, if both integers are equal  int1 -ne int2 true, if both integers are not equal  int1 -le int2 true, if int1 is less than or equal to int2  int1 -ge int2 true, if int1 is greater than or equal to int2  int1 -lt int2 true, if int1 is less than int2  int1 -gt int2 true, if int1 is greater than int2 To test files and/or folders (directories):  -e “file” true, if “file” exists  -f “file” true, if “file” exists and it is a regular file (not a directory)  -d “file” true, if “file” exists and it is a directory  -s “file” true, if “file” exists and it is not empty (size is not zero)  -r “file” true, if “file” exists and is readable (has the read permission set)  -w “file” true, if “file” exists and is writable (has the write permission set)  -x “file” true, if “file” exists and is executable (has the execute permission set) Note: “file” actually refers to a path (either relative or absolute) and may be a file or directory. Also, since files and directory names can contain spaces (or the file/directory may not exist), use double quotes around the file variable.  Logical Operators o && And – both comparisons are true (used outside the square brackets) o || Or – either comparison is true (used outside the square brackets) o ! Not – reverses the comparison result (should be used inside the square brackets) Note: There are alternatives to the AND (-a) and the OR (-o) but they are not recommended since their behaviour can be unpredictable. #!/bin/bash # An example using && and a command line argument # To execute:./scriptName.sh filename if [ -r “$1” ] && [ -s “$1” ] then echo “$1 is a usable file or directory.” else echo “$1 is either not readable, an empty file, or doesn’t exist.” fi Repetition/Looping The while Loop    while [ condition ] do statements done The while loop will continue to do the statements until the condition is false The condition can use any of the comparison operators listed above (in the if-else section) Note proper spacing – spaces around the square brackets and do/done are each on their own line The until Loop     until [ condition ] do statements done The until loop is the opposite of the while…continue to do the statements until the condition is true The same comparison operators and spacing as the while loop Why this loop? Sometimes the code can be more readable if you loop until something is true that if it is false. The until loop is not as commonly used as the while loop The for Loop    for loopVariable in listOfValues do statements (that most often use the loopVariable) done The for loop will take each item in the listOfValues (in order, one after the other), assign that item as the value of the loopVariable, execute the commands between do and done then go back to the top, grab the next item in the listOfValues and repeat. (Similar to the foreach loop in C# - the listOfValues is a bit different.) The listOfValues can be a list of strings separated by spaces: o Example: names=“Cliff Dave Marsha” then you would use $names as your listOfValues The listOfValues can be a range of numbers inside curly brackets (with no spaces):  o Example: for number in {1..5} will start at 1 and end at 5, incrementing by one each time o Example: for number in {5..1} will start at 5 and end at 1, decrementing by one each time o Example: for number in {0..10..2} will start at 0 and end at 10, incrementing by two each time The listOfValues can be set of files using the * wildcard: o Example: for htmlFile in $1/*.html will go through the directory stored in $1, and loop through each html file. break and continue   The break keyword is used to exit a loop early – often used with if logic The continue keyword is used to skip the rest of the statements for the current iteration of the loop and start the next iteration of the loop. #!/bin/bash # An example using continue and a command line argument # To execute:./scriptName.sh directoryToProcess # loop through all the files in the directory specified on the command line # using wildcard * with the 1st command line argument for value in “$1”/* do # if the file does not have the read permission set if [ ! -r “$value” ] then echo “$value is not a readable file” continue fi # for a readable file, copy it do a backup directory cp “$value” “$1/backup/” done

Use Quizgecko on...
Browser
Browser