ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

How can i write bash shell scripting ?

user-image
تم إضافة السؤال من قبل Amar Singh Yadav
تاريخ النشر: 2013/10/06
Athanassios Staveris-Polykalas
من قبل Athanassios Staveris-Polykalas , Secretary General of Telecommunications and Post - Hellenic Republic , General Secretariat of Telecommunications and Post - Hellenic Republic

Hi, answering in few lines is impossible.

check this tutorial for begginers it's excellent

http://linuxcommand.org/writing_shell_scripts.php

Sreenath MT
من قبل Sreenath MT , Senior IT Engineer , Ramniklal B Kothary

The default shell in linux is the very commonly used and highly versatile "bash".  A bash script is a file containing a list of commands to be executed by the bash shell.

A simple bash shell scripting programme

 

USE vi command or vim editor  to create a new page and write the following commands

 

  • #! /bin/bash 
  • # script to turn the screen blue
  • setterm -background blue
  • echo It is a blue day   

 

1st line  specifies which shell be used to interpret the commands in the script. 

 

2st line: is a comment (has no effect when the script is executed).

3st line: sets the background colour.

4st line: displays a message.

 

Save it with a name background, The script is complete and ready to run. scripts are run by typing a dot and slash before file name .for example here we created a script with name background try to execute the script file by typing

 

  • . /background

 

 

even if we type like this  the scrpit will  not run since we have not added permissions for the the file to be get execute.

 

Give permission by typing chmod command 

 

  • chmod755 background (make sure you are in the same folder or else link to the folder )

 

after it please try

 

  • . /background

 

and see the script running.

 

I think you get a clear idea about the bash scripting.please reply if you need any further assistance.

 

Regards ,

 

Sreenath

Mohamed Magdy
من قبل Mohamed Magdy , System Administrator & Owner , ModServ, LLC.

First of all you must have the right OS.

 

I use CentOS5/6 but in the meantime6 is better.

 

You can start the code by this:

#!/bin/bash

 

then you could write the commands the way you wish, Let's write for example a bash script to compress a file then move it from /home/FILE.tar.gz to /backup/FILE.tar.gz

#!/bin/bash

tar -cpzf /home/FILE /home/FILE.tar.gz

mv /home/FILE.tar.gz /backup/

 

Simple, huh?

BTW, this two commands can be written in one line with a semicolon as a separator.

 

#!/bin/bash

tar -cpzf /home/FILE /home/FILE.tar.gz; mv /home/FILE.tar.gz /backup/

 

Also for your knowledge, bash is way too easy than PHP, in PHP you have to end the line by a semicolon but in bash Enter is enough.

 

FYI, the last two commands can be performed in one command only but I wrote them in this way to just explain my point.

 

If you need any other examples or any help just let me know

 

Best wishes.

مستخدم محذوف‎
من قبل مستخدم محذوف‎

FYI

 

bash is a flavor of C-language. if you are good in C-language things are much easier

conditional argument can be done using IF... , IF...THEN or IF---THEN.....ELSE etc

Loops can be done by   FOR command

 

you can run in from the shell by simply typing (assume test.sh is the file name)

# ./test.sh

most of the system coomands of linux work for bash script

 

cheers

Lijo

مستخدم محذوف‎
من قبل مستخدم محذوف‎

use CentOS5/6 but in the meantime6 is better.

 

You can start the code by this:

#!/bin/bash

 

then you could write the commands the way you wish, Let's write for example a bash script to compress a file then move it from /home/FILE.tar.gz to /backup/FILE.tar.gz

#!/bin/bash

tar -cpzf /home/FILE /home/FILE.tar.gz

mv /home/FILE.tar.gz /backup/

 

Simple, huh?

BTW, this two commands can be written in one line with a semicolon as a separator.

 

#!/bin/bash

tar -cpzf /home/FILE /home/FILE.tar.gz; mv /home/FILE.tar.gz /backup/

 

Also for your knowledge, bash is way too easy than PHP, in PHP you have to end the line by a semicolon but in bash Enter is enough.

مستخدم محذوف‎
من قبل مستخدم محذوف‎

There's a lot of tutorials out there to read on how to bash script.

Select any editor you want to use (e.g. like vim, nano or pico)

Script will start with;

 

Simple script on how to display directory with colors

 

#!/bin/bash

echo "Welcome to Bash Shell Scripting!";

ls -FGAl

echo "Display directory with color";

 

Save it as "display_color.sh"

 

Convert it as executable 

# chmod +x display_color.sh

 

To execute the script

# ./display_color.sh

 

This is a simple example of bash shell scripting.

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟