Are you trying to run a script in Linux operating system using command line options?
This guide this for you.
A shell is a command-line interpreter and typical operations performed by shell scripts include file manipulation, program execution, and printing text.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux related tasks.
In this context, we shall look into how to execute a Script In Linux.
A shell script is a computer program designed to be run by the Unix/Linux shell which could be one of the following:
i. The Bourne Shell
ii. The C Shell
iii. The Korn Shell
iv. The GNU Bourne-Again Shell
Shell scripts have several required constructs that tell the shell environment what to do and when to do it. Of course, most scripts are more complex than the above one.
The shell is, after all, a real programming language, complete with variables, control structures, and so forth. No matter how complicated a script gets, it is still just a list of commands executed sequentially.
By default, the shell script will not run. You need to set execute permission for your shell script. To execute or run script type the following command:
chmod +x script-name-here
OR
chmod 0755 script.sh
Next, use the ls command to view permission on the script:
$ ls -l script-name-here
Now, to execute the script, simply run the following command;
$ ./script-name-here
You can also run a script using any one of the following syntax:
$ /path/to/shell/script/backup.sh
To create a bash script, you can simply use the nano command in Ubuntu or the vim command in CentOS.
For example, lets say you want to create a script with name "name.sh"
TO do this, execute;
nano name.sh
OR
vim name.sh
This will open up the file where you can input any code which will run when initiated.
After adding the code, you can proceed with saving and closing the file.
Also, Set the permission using the chmod command:
$ chmod +x name.sh
To run a script, simply run the command;
$ ./name.sh