Bash – Running Script

Background

Let us review the choices we have for running our bash scripts.

 

Outline

  1. filename ( script.sh )
  2. full file name ( <folder>\<filename>)

Tasks

Filename ( script.sh )

Syntax


[filename]

Sample


helloWorld.sh

Output

Output – Image

Output – Text


bash: helloWorld.sh: command not found...

Explanation

  1. Error reads:- “Command not found

Full Filename ( script.sh )

Syntax


[folder]/[filename]

Sample


./helloWorld.sh

Output

Output – Image

Output – Text


bash: ./helloWorld.sh: Permission denied

Explanation

  1. Error Message
    • Permission Denied

Troubleshooting

Command – Filename ( script.sh )

The issue is likely traced to the fact that the script’s path is not in your folder.

Review Your Path

Export Command

Syntax

export

Sample

export | grep 'export'

Output
Output – Image

Output – Text

export | grep "PATH"
declare -x PATH="/home/dadeniji/.local/bin:/home/dadeniji/bin:/home/dadeniji/.local/bin:/home/dadeniji/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/usr/local/go/bin:/usr/local/go/bin"
declare -x WINDOWPATH="2"
>

Explanation
  1. The script that our bash script is located in is not in our path

 

Check File’s Execution Permission

ls Command

Syntax

ls -la <file>

Sample

ls -la helloWorld.sh

Output
Output – Image

Output – Text

>ls -la helloWorld.sh 
-rw-r--r--. 1 dadeniji dadeniji 365 Nov 26 18:40 helloWorld.sh
>

 

Explanation
  1. On issuing “ls -la”
    • We see that the execute bit is not signed for self, group, nor public

Remediation

  1. sh <fullfilename> (  Initiate sh and pass along file’s full name )
  2. filename ( mark executable )

sh Filename ( script.sh )

Syntax


sh [folder]\[filename]

Sample


sh ./helloWorld.sh

Output

Output – Image

Output – Text


>sh ./helloWorld.sh
Hello dadeniji, it is 2020-11-27 11:23 AM
>

Explanation

  1. We are good

filename ( mark executable )

chmod

chmod – Change File Mode

Syntax


chmod +x [folder]\[filename]

Sample


chmod +x ./helloWorld.sh

Output

Output – Image

 

Output – Text

>chmod +x helloWorld.sh 
>

 

Review File – ls

Syntax


ls -la [filename]

Sample


ls -la *.sh

Output

Output – Image

Output – Text

>ls -la *.sh
-rwxr-xr-x. 1 dadeniji dadeniji 348 Nov 27 11:24 helloWorld.sh
>

Explanation

  1. The execute option is now availed to the file

 

Initiate File ( filename )

Syntax


[filename]

Sample


./filename.sh

Output

Output – Image

Output – Text
>./helloWorld.sh
Hello dadeniji, it is 2020-11-27 11:39 AM
>

Explanation

  1. With the execute option, we are able to initiate the file

 

Summary

Unix\Linux is inherently secure.

Here are the requirements to execute a file:-

  1. sh <filenamefull>
  2. grant execute permission to file

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s