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
Method2: use sh command to execute it.
sh scriptname or bash scriptname or ksh scriptname
Method3: Use source command to execute a script.
Note: source command does not require shell script to have executable permissions for executing it.
Method4: Giving entier PATH for the script when executing
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)
- 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
- wget download a file to a directory in Linux/Unix - June 4, 2018
- GIT: How to compare two GIT branches? - June 3, 2018
- Online training on Linux Bash shell scripting - February 8, 2018