• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » How to Write Shell Script

By Abhishek Ghosh May 22, 2014 7:29 am Updated on May 22, 2014

How to Write Shell Script

Advertisement

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 :

Vim
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 :

Advertisement

---

Vim
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 :

Vim
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.

How to Write Shell Script

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 :

Vim
1
echo 'Hello, world!'

It will print :

Vim
1
Hello, world!

So, if we make our script a bit complex, it can look like :

Vim
1
2
3
4
5
6
#!/bin/sh
clear
ls -al
 
echo 'Hello, all done!'

echo appears easy, but not so, do it :

Vim
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:

Vim
1
exit 4

Conditional expression can be :

Vim
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 :

Vim
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
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How to Write Shell Script

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • Unix Shell or Shell

    Unix Shell or Shell refers to the traditional user interface under Unix or Unix-like computer operating systems. User can type a command type, which is executed by the computer.

  • Adding Help & Option in Bash Script

    Many New Hobbyist Programmers and Students Write Bash Scripts. Adding Help & Option in Bash Script is Easy. Here is Basic Guide With Example.

  • WordPress & PHP : Different AdSense Units on Mobile Devices

    Here is How To Serve Different AdSense Units on Mobile Devices on WordPress With PHP. WordPress Has Function Which Can Be Used In Free Way.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Market Segmentation in BriefSeptember 20, 2023
  • What is Booting?September 18, 2023
  • What is ncurses?September 16, 2023
  • What is JTAG in Electronics?September 15, 2023
  • iPhone 15 Pro Max Vs Samsung Galaxy S22/S23 UltraSeptember 14, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy