Shell scripting for fun and profit

Linux - The Productive Programmer's Best Friend Productive shells: Reinvent the way you work
9 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$49.99
List Price:  $69.99
You save:  $20
€47.39
List Price:  €66.35
You save:  €18.96
£39.48
List Price:  £55.27
You save:  £15.79
CA$69.93
List Price:  CA$97.91
You save:  CA$27.98
A$76.86
List Price:  A$107.61
You save:  A$30.75
S$67.15
List Price:  S$94.02
You save:  S$26.86
HK$389.09
List Price:  HK$544.76
You save:  HK$155.66
CHF 44.28
List Price:  CHF 61.99
You save:  CHF 17.71
NOK kr552.48
List Price:  NOK kr773.52
You save:  NOK kr221.03
DKK kr353.46
List Price:  DKK kr494.87
You save:  DKK kr141.41
NZ$85.02
List Price:  NZ$119.04
You save:  NZ$34.01
د.إ183.61
List Price:  د.إ257.07
You save:  د.إ73.45
৳5,952.52
List Price:  ৳8,334.01
You save:  ৳2,381.48
₹4,217.70
List Price:  ₹5,905.12
You save:  ₹1,687.41
RM223.53
List Price:  RM312.96
You save:  RM89.43
₦83,723.75
List Price:  ₦117,219.95
You save:  ₦33,496.20
₨13,842.48
List Price:  ₨19,380.58
You save:  ₨5,538.10
฿1,735
List Price:  ฿2,429.14
You save:  ฿694.14
₺1,722.82
List Price:  ₺2,412.08
You save:  ₺689.26
B$289.31
List Price:  B$405.06
You save:  B$115.74
R905.17
List Price:  R1,267.31
You save:  R362.14
Лв92.66
List Price:  Лв129.73
You save:  Лв37.07
₩69,838.72
List Price:  ₩97,779.80
You save:  ₩27,941.07
₪186.81
List Price:  ₪261.55
You save:  ₪74.74
₱2,950.38
List Price:  ₱4,130.77
You save:  ₱1,180.39
¥7,786.65
List Price:  ¥10,901.93
You save:  ¥3,115.28
MX$1,010.13
List Price:  MX$1,414.26
You save:  MX$404.13
QR181.66
List Price:  QR254.33
You save:  QR72.67
P677.69
List Price:  P948.83
You save:  P271.13
KSh6,473.70
List Price:  KSh9,063.70
You save:  KSh2,590
E£2,484.50
List Price:  E£3,478.50
You save:  E£994
ብር6,130.69
List Price:  ብር8,583.46
You save:  ብር2,452.76
Kz45,623.90
List Price:  Kz63,877.12
You save:  Kz18,253.21
CLP$48,554.78
List Price:  CLP$67,980.58
You save:  CLP$19,425.80
CN¥362.25
List Price:  CN¥507.18
You save:  CN¥144.93
RD$2,999.87
List Price:  RD$4,200.06
You save:  RD$1,200.18
DA6,670.19
List Price:  DA9,338.81
You save:  DA2,668.61
FJ$113.51
List Price:  FJ$158.92
You save:  FJ$45.41
Q384.54
List Price:  Q538.38
You save:  Q153.84
GY$10,416.21
List Price:  GY$14,583.53
You save:  GY$4,167.31
ISK kr6,885.62
List Price:  ISK kr9,640.42
You save:  ISK kr2,754.80
DH497.67
List Price:  DH696.78
You save:  DH199.10
L909.82
List Price:  L1,273.82
You save:  L364
ден2,912.40
List Price:  ден4,077.60
You save:  ден1,165.19
MOP$399.33
List Price:  MOP$559.09
You save:  MOP$159.76
N$900.58
List Price:  N$1,260.89
You save:  N$360.30
C$1,833.12
List Price:  C$2,566.52
You save:  C$733.39
रु6,730.44
List Price:  रु9,423.16
You save:  रु2,692.71
S/189.03
List Price:  S/264.65
You save:  S/75.62
K200.43
List Price:  K280.62
You save:  K80.18
SAR187.67
List Price:  SAR262.75
You save:  SAR75.08
ZK1,376.04
List Price:  ZK1,926.57
You save:  ZK550.52
L235.82
List Price:  L330.17
You save:  L94.34
Kč1,197.77
List Price:  Kč1,676.98
You save:  Kč479.20
Ft19,418.42
List Price:  Ft27,187.34
You save:  Ft7,768.92
SEK kr549.81
List Price:  SEK kr769.78
You save:  SEK kr219.97
ARS$50,077.77
List Price:  ARS$70,112.88
You save:  ARS$20,035.11
Bs345.44
List Price:  Bs483.65
You save:  Bs138.20
COP$220,323.37
List Price:  COP$308,470.35
You save:  COP$88,146.97
₡25,320.40
List Price:  ₡35,450.59
You save:  ₡10,130.18
L1,258.58
List Price:  L1,762.11
You save:  L503.53
₲387,944.27
List Price:  ₲543,153.02
You save:  ₲155,208.75
$U2,137.07
List Price:  $U2,992.07
You save:  $U855
zł205.58
List Price:  zł287.83
You save:  zł82.25
Already have an account? Log In

Transcript

Pipes and sub shells are one way of expanding the capabilities of our shell. The ultimate way is by writing shell scripts. These scenarios must be taken into consideration when dealing with complex tasks that can't be automated with a one line command. The good news is that almost any task can be automated with the use of shell scripts. We won't go over an introduction to shell script and instead, we will be looking at some more advanced use cases for writing them. Enough talking and let's start our journey into shell scripting.

First thing that's open a file called script.sh and split the screen so that you can test while writing. Every shell should start with the shebang command, which is which are these characters and followed by the interpreter. It will our shell script will use we will be using bash is our default interpreter. And it's a good idea to use bash because it's a common interpreter that comes with most Linux distributions, and also our six. So shebang being bash. Let's start with a simple use case, reading the arguments passed into the command line, we will assign the value of the first our command line argument, dollar one to a variable called arc, and then print it back to the screen.

Let's save our script and assign it execution permissions and then run it with one argument. As you can see, the default value test is printed back to the screen. In some cases, we want to assign default values to variables. In order to do this, add this to the variable assignment followed by the default value. Now if we rerun the script, we can see that passing no arguments, we'll call that the value, default value actually. And just like pipes, we can change multiple default value assignments together, we can define another variable, or ox, assign it the value 123.

And use the same syntax to assign its value to the arc variable before using the default value script like so. In this case, arc will always receive 123 as its default value. Now let's look at string selectors. To select the substring use the colon followed by the starting position, followed by another colon followed by how many characters like this. In our case, we will be selecting four characters starting from character five. After running the script, we can see the default value long printed to the screen.

Most shell scripts are designed to be run from the command line and receive a variable number of arguments In order to read command line arguments without knowing the total number of arguments, we use the while statement that checks the first argument to not be now using the minus set conditional expression, which means to not be equal to zero. In the while loop, let's echo the variables value and run shift, which shifts command line arguments one position to the left. If we run our script with the arguments, ABC, we can see that our while loops through the parameters and printed each one on a separate line. Now let's extend our key arguments parser and add the case statement for interpreting the argument. Let's assume that our script will have a help option. parsing standard recommends doing long arguments version with minus minus and short version with only one minus so both H and help will bring The Help message.

Also it is recommended to always have a default case and print the message when the user sent invalid options and then exit with a non zero exit value. If we run our script with the minus h option, we can see the message printed. Same as minus minus help. If we run the script with any other option, the invalid option text is printed and the script exits with the exit code one. To get the exit code of the last command use dollar question mark for your Echo. Now let's look at basic functions in Shell.

The syntax is pretty similar to other programming languages. Let's write the function called print IP that will print the IP of the interface specified as the first argument and we will use a subshell and assign the value to a variable called IP. We already have the full command inside our clipboard It's the same one we saw in the lesson about pipes. Now let's add another case store switch statement for the minus E or minus minus A p option. The option will be followed by the name of the interface, which will then pass to the print IP function. Having two arguments for one option means we need to call the shift command twice.

Let's do an IP config to get the name of our wireless interface. And now let's run the following command. Okay, and we can see the IP address. This is a very basic use case where we can see how command line arguments can be passed. We can add our limited options store case statement, defined functions for handling the arguments and even chain multiple options together to form complex scripts and receive well structured information as command line arguments. being effective means running tasks fast fast.

And when it comes to speed bash is not really the first choice in terms of script interpreters. Luckily, we still have some tricks up our sleeves. So if a shell script needs to run multiple independent tasks, we can use the end simple to send the process in background and move forward to the next command. Let's create two functions long running task one and two and and sleep colony side to simulate a long running task. The first long running task function will be sleeping one second and the next will be sleeping two seconds. Then for testing purposes, let's add another key store switch statement called minus p or minus minus parallel and Run the tool long running tasks.

Now if we run this command, it will Take a total of three seconds for the script to finish, we can measure this with the time utility. If you run both functions in the background, we can reduce the running time to the longest running time of both functions because of the weight. When running long running tasks, we probably want the script to wait for the longest running task to finish. In our case, task two. We can achieve this by grabbing the PID of the second task. Here we use the dollar exclamation mark to grab the PID of the last friend command.

Then we use the wait built in command to wait for the execution to finish. After running the script again with the time utility, we can see it takes us a total of two seconds to complete the task. Who would have thought we can do parallel processing in Shell. In the case that the executions take a longer time we can add up notification when the script finishes, notify sent script.sh execution finished. This way, we can start the script to work on some other tasks and receive a notification when the script finishes, you can let your imagination go wild on the things you can achieve with parallel processing, and notifications. Through this lesson, we have seen some common shell predefined variables.

They were this for first argument. This returns the code of the last command, and this is the fifth of the last command run. Other commonly used shell predefined variables include the number of parameters, the list of parameters, all the parameters, the name of the shell script, or the script and the piece of the current running shell. Bash has a lot of features and we recommend going through its man page too. get more information about them. shell scripts are amazing when used the right way.

They can find you in system commands like we saw in our example when we got only the IP address without the whole IP config output, and much much more. You as a pragmatic terminal user should identify what tasks you most commonly do in the command line and what can be automated using shell script. You should create your own collection of shell scripts and add them to your path so that they are easily accessible from any directory.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.