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 :
1 2 | 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 :
---
1 2 3 4 5 6 7 8 9 10 11 | ## method 1 sh script.sh # or change to bash by typing bash and hitting the return key ## method 2 bash script.sh # read documentation man sh # or run more easily ## method 3 chmod +x script.sh ./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 :
1 2 3 4 | #!/bin/sh clear ls -al |
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 :
1 | echo 'Hello, world!' |
It will print :
1 | Hello, world! |
So, if we make our script a bit complex, it can look like :
1 2 3 4 5 6 | #!/bin/sh clear ls -al echo 'Hello, all done!' |
echo appears easy, but not so, do it :
1 | echo ~/*.txt |
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:
1 | exit 4 |
Conditional expression can be :
1 2 3 4 5 | #!/bin/bash if [[ -e any.txt ]] ; then cp any.txt where.txt fi |
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 :
1 | https://gist.github.com/AbhishekGhosh/46b84409930fbbc97f6b |
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.
Tagged With shell , how to write a shell script in windows , how to write a simple shell script for Ubuntu 16 04 , how to write in windowsshell , how to write shell script code in window 8 , how to write shell script in windows pc , sea shell photo , write shell , writing windows shell scripts