|
| 1 | +""" |
| 2 | +In this chapter, you’ll learn how to create basic string and numeric variables, and perform calculations on these variables. |
| 3 | +You’ll also learn about the magic of a shell-within-a-shell (shell-ception), opening up huge opportunities for advanced scripting. |
| 4 | +""" |
| 5 | + |
| 6 | +""" |
| 7 | +*** Basic variables in Bash |
| 8 | + >>>>>> var1="moon" : asign a variable |
| 9 | + >>>>>> echo $var : reference variable with notation |
| 10 | + |
| 11 | + *** single, double, backticks |
| 12 | + >>>>>> 'text' : literally what's in between |
| 13 | + >>>>>> "$text or `text` o $(text)" : literally except using [$] and [backticks], just to be printed out |
| 14 | + >>>>>> `text` : runs and captures STDOUT back into variable |
| 15 | + |
| 16 | + eg: var="the day is `date`" |
| 17 | + echo $var |
| 18 | + |
| 19 | + the day is 02/11/22 16:51 |
| 20 | + |
| 21 | +""" |
| 22 | + |
| 23 | +""" |
| 24 | +### Using variables in Bash |
| 25 | +
|
| 26 | +-Create a variable, yourname that contains the name of the user. Let's use the test name 'Sam' for this. |
| 27 | +-Fix the echo statement so it prints the variable and not the word yourname. |
| 28 | +-Run your script. |
| 29 | +
|
| 30 | +""" |
| 31 | +# Create the required variable |
| 32 | +yourname="Sam" |
| 33 | + |
| 34 | +# Print out the assigned name (Help fix this error!) |
| 35 | +echo "Hi there $yourname, welcome to the website!" |
| 36 | + |
| 37 | + |
| 38 | +repl:~/workspace$ bash script.sh |
| 39 | +Hi there Sam, welcome to the website! |
| 40 | + |
| 41 | +""" |
| 42 | +### Shell within a shell |
| 43 | + |
| 44 | +-Which of the following correctly uses a 'shell within a shell' to print out the date? |
| 45 | + We do not want to select the option that will just print out the string 'date'. |
| 46 | + |
| 47 | +Answer : echo "Right now it is `date`" |
| 48 | +
|
| 49 | +""" |
| 50 | + |
| 51 | +""" |
| 52 | +### Numeric variables in Bash |
| 53 | +
|
| 54 | +*** for num vars we must use : |
| 55 | + >>>>>> expr |
| 56 | + |
| 57 | + eg : expr 5+4 |
| 58 | + 9 |
| 59 | + |
| 60 | + *** expr LIMITATIONS : don't handle decimals |
| 61 | +*** for decimal vars : |
| 62 | + >>>>>> bc (basic calculator) |
| 63 | + |
| 64 | + eg: |
| 65 | + # echoing and piping bc |
| 66 | + echo "5 + 7.5" | bc |
| 67 | + 12.5 |
| 68 | + |
| 69 | + # echo calculation piped to bc for filling string results |
| 70 | + model1=87.65 |
| 71 | + model2=89.20 |
| 72 | + echo "Score is $(echo "model1 + model2" | bc)" |
| 73 | + score is 176.85 |
| 74 | + |
| 75 | +*** for specifying decimal places : |
| 76 | + >>>>>> scale |
| 77 | + |
| 78 | + eg: echo "scale=3; 10/3" | bc |
| 79 | + 3.333 |
| 80 | +""" |
| 81 | + |
| 82 | +""" |
| 83 | +### Converting Fahrenheit to Celsius |
| 84 | +
|
| 85 | +-Your task is to write a program that takes in a single number (a temperature in Fahrenheit) as an ARGV argument, converts it to |
| 86 | + Celsius and returns the new value. There may be decimal places so you will need to undertake calculations using the bc program. |
| 87 | +-At all times use 2 decimal places using the scale command for bc. |
| 88 | +-The formula for Fahrenheit to Celsius is: |
| 89 | +
|
| 90 | +The formula for Fahrenheit to Celsius is: |
| 91 | +C = (F - 32) x (5/9) |
| 92 | +
|
| 93 | +""" |
| 94 | +# Get first ARGV into variable |
| 95 | +temp_f=$1 |
| 96 | + |
| 97 | +# Subtract 32 |
| 98 | +temp_f2=$(echo "scale=2; $temp_f - 32" | bc) |
| 99 | + |
| 100 | +# Multiply by 5/9 |
| 101 | +temp_c=$(echo "scale=2; $temp_f2 * 5 / 9" | bc) |
| 102 | + |
| 103 | +# Print the celsius temp |
| 104 | +echo $temp_c |
| 105 | + |
| 106 | + |
| 107 | +repl:~/workspace$ bash script.sh 108 |
| 108 | +42.22 |
| 109 | + |
| 110 | +""" |
| 111 | +### Extracting data from files |
| 112 | + |
| 113 | +Your task is to extract the data from each file (by concatenating) into the relevant variable and print it out. |
| 114 | +The temperature in the file region_A needs to be assigned to the variable temp_a and so on. |
| 115 | + |
| 116 | +-Create three variables from the data in the three files within temps by concatenating the content into a variable using a shell-within-a-shell. |
| 117 | +-Print out the variables to ensure it worked. |
| 118 | +-Save your script and run from the command line. |
0 commit comments