This is a small how-to topic on running the shell script you just received or created. The basic philosophy of Linux is to consider everything as files. And if you want to execute a file/app the permissions are set to executable on that file otherwise you will get permissions denied.
Let’s try to execute a script without setting executable permissions to the shell script file and see what happens?.
./numper.sh bash: ./numper.sh: Permission denied
So before executing a shell script it’s advisable to change the permissions to executable. Here are the steps to execute your shell script.
Step1: Change the permissions of shell script to executable.
chmod +x shellscriptfile
Note1: The above command will give execute permissions to everyone. If you don’t want to give execute permissions to all and want to give execute permission to owner you can use below command
chmod u+x shellscriptfile
Note2: There is no need to have .sh as extension, just execute permissions are required to execute a script.
Note3: It’s not advisable to give 777 permissions to a script to execute it.
Step2:Now run the script
Method1:Go to script location and execute as blow
./scriptname
Method2: use sh command to execute it.
sh scriptname or bash scriptname or ksh scriptname
Method3: Use source command to execute a script.
source scriptname
Note: source command does not require shell script to have executable permissions for executing it.
Method4: Giving entier PATH for the script when executing
/path/to/my/script
Method5:Setting PATH variable to your scripts location.
Stay tuned to our next post on how to debug your shell scripts.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018