Linux Shell Scripts
- Anne Shroble
- May 2, 2016
- 3 min read
1. What Is Shell Scripting?
Before I go into Shell Scripting, let’s find out what a shell is.
Well, the shell is a program that takes commands from the keyboard and gives them to the operating system to perform. People keep talking about the old days; well I spent two semesters trying to learn COBAL in DOS - “In the old days”. Nowadays, we have graphical user interfaces (GUIs) in addition to command line interfaces (CLIs) such as the shell. I’ve also learned some Auto Cad using DOS based command lines. Sometime this was much simpler than GUI.
Most Linux systems run a program called bash (Bourne Again Shell), an enhanced version of the original Unix shell program, sh, written by Steve Bourne; which acts as the shell program. Besides bash, there are other shell programs that can be installed in a Linux system. These include: ksh, tcsh and zsh.
2. And so what is Shell Scripting? Basically shell scripting is using program commands and a text editor to place these commands into a small file called script. The more one uses the command lines and realizes what they do, the easier it is to use them for program manipulation. I believe we used to make “Macros” using basic operations and combining them into small programs.
Scripting allows the use of programming functions – such as ‘for’ loops, if/then/else statements, and so forth – directly within your operating system’s interface. Learning another language is non-essential because you’re using what you already know: the “good old” command-line.
That’s really the power of scripting, getting to program with commands you already know while learning staples of most major programming languages. If you have something repetitive and tedious? You can just script it! Want a shortcut for a really complex command? Script it. Want to build a really easy way to use command-line interface for something? Script it!
3. What is the installation process? Good Question. I’ve been searching and searching… one doesn’t install the script which is kind of what I figured, one executes .sh. It is a shell script. It doesn't need to be "installed". To begin your script type !/bin/bash at the # or prompt. The script can also be written in an editor and then inserted into the .bashrc file.
4. What are loops, lists, and functions?
Loops are scripts which can cause a program to repeat itself. By using the while, if-then, and until commands one can make a task work until it has run out. Something like ping 192.168.20.100 until count = 5. This will make ping go for five times. Don’t quote my syntax, I haven’t done any programming for a few years. Hopefully you get the drift. While and if-then also work in the same manner.
A list is just that, a command using various list operators. The following operators can be used to create a list command:
Semicolon (;)
Ampersand (&)
Double Ampersand (&&)
Double vertical line or pipe (| |)
For an example: HOSTNAME=carson.example.com ; export HOSTNAME. Both HOSTNAME and export commands are executed, however sequentially. In other words, one after the other.
A function is part of a script that performs a specific subtask and can be called from other parts of the script by using its name. By placing a set of parentheses after the function name (), and using curly braces {} to enclose the syntax which make up the function you are well on your way to writing script.
While searching for a nonexistent answer, I found some short cuts which had been placed on a blog. The only one that made any sense to me was the following.
Mac Maha November 27, 2012, 9:58 am
I used it this way. I added myself to visudo file with nopasswd privileges. so that I don’t have to type password when I do “sudo su -“. Then created alias root=’sudo su -‘ This enables me to log in to root with just “root”.
by the ways the article is very helpful for everyone who works on Linux servers or desktops on everyday basis. Regards, Mac Maha.
Found on the website of:
30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X. (n.d.). Retrieved May 02, 2016, from http://www.cyberciti.biz/tips/bash-aliases-mac-centos-linux-unix.html
I don’t want to put something out for you all that I don’t understand. So in closing, hope this blog is helpful. Linux can be fun, but if one doesn’t do some "serious study of the subject"; well, I guess this goes for any subject. Spend enough time at anything, and be surprised at how proficient you can become.
My other reference used was:
Working With Commands. (n.d.). Retrieved May 02, 2016, from http://linuxcommand.org/lc3_lts0060.php
Recent Posts
See AllIt's been a long time since visiting this site. I've been searching for employment, haven't found the appropriate match for me. I've...
Hey, hey, hey, look who’s back! Harry N. Hacker here. The boss and Anne are on vacation this week, so I’m going to tell you about my...
Good afternoon to all you happy blog followers. Today we are going to investigate a software package called Moosoft’s The Cleaner. The...
Comments