Writing Shell Script is interesting, shell scripts are powerful, performs very simple to quite complex set of commands. You must know is what is Unix Shell. We talked about quite closer thing – cron. A shell script is a computer program which uses scripting language. A scripting language is usually interpreted from source code or byte code. We are only discussing How to Write Shell Script for UNIX and Unix like OS.
How to Write Shell Script : Basics
When we are using iTerm2 on Mac, we get this dialogue when we open the program :
Last login: Wed May 21 22:59:43 on console
If the written Shell Script’s name is
script.sh, we need to execute it in these ways :
## method 1
# or change to bash by typing bash and hitting the return key
## method 2
# read documentation
# or run more easily
## method 3
chmod +x script.sh
Typical we use shell scripts for file manipulation, program execution and printing text on terminal or ssh. Running an unknown shell script is quite dangerous. At least for this sake, one must know the basic about the Shell Scripts. A very basic shell script will be :
You can copy paste it as plain text and save it as
script.sh and execute it. It will return a list of all the files and directories where you are, after clearing the screen. The last login kind of text will get vanished and only the list will show up.
Perhaps the biggest advantage of writing a shell script is that the commands and syntax are exactly the same as those directly entered at the command line.
How to Write Shell Script : Advanced Usage
Normally on Terminal or iTerm, if we write and hit the rerun key :
echo 'Hello, world!'
It will print :
So, if we make our script a bit complex, it can look like :
echo 'Hello, all done!'
echo appears easy, but not so, do it :
When a process completes, it returns a small non-negative integer value, called its exit status. By convention, it returns zero if it completed successfully and a positive number if it failed with an error. A Bash script can obey this convention by using the built-in command exit. The following command:
Conditional expression can be :
if [[ -e any.txt ]] ; then
cp any.txt where.txt
Almost all unix commands works but you must know some scripting language to create complex shell scripts. Like, this is a bit complex, which wget the lastet wordpress and do all the steps we usually type to install wordpress :
Most paid software has kind of bash script for automated actions. You will ask why we do not use a script to install LAMP server, install wordpress but type commands. This is because, we need to see the errors in real time and correct them. More complex example of converting JPEG images to PNG images, where the image names are provided on the command line”possibly via wildcards”instead of each being listed within the script, can be created with this file, typically saved in a file like /home/username/bin/jpg2png :
The jpg2png command can then be run on an entire directory full of JPEG images with just /home/username/bin/jpg2png *.jpg
This amount of knowledge is enough to perform basic works.
Follow the Author of this article :