Make

Make a Bash Script Executable


1) Create a new text file with a .sh extension.

I created a new file called deploy.sh for my website.

2) Add #!/bin/bash to the top of it.

This is necessary for the “make it executable” part.

3) Add lines that you’d normally type at the command line.

As an example, here’s the full contents of the file I use to deploy general updates to andrewcbancroft.com

1 #!/bin/bash23 hugo45 git add .67 git commit -m "Updates"89 git push

4) At the command line, run chmod u+x YourScriptFileName.sh

I ran chmod u+x deploy.sh to make mine executable.

# chmod u+x deploy.sh

#  chmod 755 deploy.sh

5) Run it whenever you need!

Now, whenever I deploy changes to my website, I run ./deploy.sh and boom. Done.

------------X-----------------


#!/bin/bash

OR

#!/bin/sh



Let’s create a basic shell script using vi

$ vi basic_script.sh

This will take you to the vi editor. Add the following lines:

#!/bin/bash

whoami

date

This simple script should display the current user followed by the date.

To save and exit the vi editor:

By default, the creator of the script does not get executable permission for the file.

To change that:

$ chmod +x basic_script.sh

This will give you (current user) the permission to execute the file.

To run the script :    $  bash basic_script.sh

The first line of output corresponds to ‘whoami’ command and the second line to ‘date’ command.

Another way of running the script is :  $ ./basic_script.sh

Running the file this way might require the user to give permission first. Running it with ‘bash’ doesn’t require the permission.

The same script is running with ‘bash’ before it, but having permission issues when tried to execute directly. The reason this is happening is that the command bash [filename] only needs read permission from the file.

Whereas the command ./[filename] run the file as an executable and hence requires the permission to execute. This question has been answered in detail on StackExchange.

In general it is better to provide executable permission.

Using variables in shell scripts

Scripts can include user-defined variables, in fact as scripts get voluminous in size it is essential to have variables that are clearly defined. Variables that are self-descriptive in nature is another quality of a good script.

Add the following lines to the script :

#!/bin/bash

#This is a comment

 

#defining a variable

GREETINGS="Hello! How are you"

echo $GREETINGS

GREETINGS is the variable defined and later accessed using ‘$’.

There should be no space in the line where variables being assigned a value.



A backup from source to destination.

$ vim backup_script.sh

                     --------

#!/bin/bashTIME=`date +%b-%d-%y`DESTINATION=/home/kashif/backup-$BACKUPTIME.tar.gzSOURCE=/data_foldertar -cpzf $DESTINATION $SOURCE                 -------

Save and close the file using :wq! and give it the executable permissions using the following command:

# chmod +x backup_script.sh

Now run the script:

# ./backup_script

Set Executable Permissions to Script

chmod u+x backup_script.sh