• 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

This Article Has Been Shared 545 Times!

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

  • Base64 Encoding and Font CSS

    Base64 Encoding Data URI can be used for Font Face in CSS Files, this is basically a good practice as we are decreasing four HTTP requests per font face.

  • Command and Control Server and Technologies

    Command and Control Server are centralized servers which are able to send commands and a part of Botnet. Botnet is group of computers to run automated program.

  • IRC Clients for OS X

    IRC Clients for OS X can be divided in to two broad categories – Free Software and Non-Free Software.Among the Non-Free Softwares there are paid softwares.

  • How To Add Ratings in Facebook Page?

    How To Add Ratings in Facebook Page, So that People or Your Customers Can See the Star Ratings Review? Here is a very simple step by step guide.

  • Swift Code for Google AdSense Publishers in India

    As Google has introduced much desired Wire Payment option, Google AdSense Publishers in India need Swift Code. Here is practical details.

Additionally, 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

  • Four Foolproof Tips To Never Run Out Of Blog Ideas For Your Website March 28, 2023
  • The Interactive Entertainment Serving as a Tech Proving Ground March 28, 2023
  • Is it Good to Run Apache Web server and MySQL Database on Separate Cloud Servers? March 27, 2023
  • Advantages of Cloud Server Over Dedicated Server for Hosting WordPress March 26, 2023
  • Get Audiophile-Grade Music on Your Smartphone March 25, 2023

About This Article

Cite this article as: Abhishek Ghosh, "How to Write Shell Script," in The Customize Windows, May 22, 2014, March 29, 2023, https://thecustomizewindows.com/2014/05/how-to-write-shell-script/.

Source:The Customize Windows, JiMA.in

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

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT