- Forums
- Linux Systems
- How To Create A Linux Shell Script - Step By Step Tutorial
on this page i will show you step by step on how to create a simple executable script using linux command shell i will give you all the commands you need to follow and at the ned you will have a linux script. [3072], Last Updated: Sat May 18, 2024
wallpaperama
Sat Feb 19, 2011
1 Comments
1768 Visits
on this page i will show you step by step on how to create a simple executable script using linux command shell i will give you all the commands you need to follow and at the ned you will have a linux script.
if you are starting to play with linux, eventually, you will find the flexibility that the linux shell provides. it provides you with so many possibilities to manage your linux box.
in this example, we are going to create a simple text file. this file will contain the commands to run. if you are familiar with linux, you know you can send commands to the shell, for example the
date command shows today's date. so we are going to save a couple of commands in a text file to run when text file is executed
ok, lets get started and login to your linux computer.
Step 1. We are going to be using the cat utility to create a simple text file. from this text file we are going to run the commands.Type the following command to create a new file:
SHELL COMMAND:
cat > new_script
Step 2. the cat command will take you to prompt mode.. here we are going to enter the shell commands to execute in our file.. so Type the following lines:
SHELL COMMAND:
echo Your files are
ls
echo today is
date
NOTE: Press ENTER to move the cursor to a new line and press CTRL-D to end
Step 3. Check the file to be sure you typed correctly without any errors. display the contents of new_script file:
SHELL COMMAND:
cat new_script
If there are errors, remove the file new_script and return to step 1 to create it again.
Step 4. Try to run the file to see if the commands will execute.
SHELL COMMAND:
new_script
You will get a 'command not found' error because the files is not executable.
Step 5. Display the permission of the file by entering:
SHELL COMMAND:
ls -la new_script
Notice that the file is not executable. You must make it executable.
Step 6. Type the following command to make new_script executable:
SHELL COMMAND:
chmod +x new_script
Step 7. To see the new permission, enter:
SHELL COMMAND:
ls -l
You now have execute permission, as well as read and write permissions for the file.
Step 8. Execute the new script by typing its name:
SHELL COMMAND:
new_script
All the commands that you typed into the file are executed, and their output is sent to the screen.
Step 9. If you receive an error message such as:
Command not found Type the following:
SHELL COMMAND:
./new_script
This command line tells the shell exactly where to find the shell script, new_script, in your current directory knows as 'dot.'
as yo can see from the script output, it lists the files in your directory and the today's date.
note, the
echo command. if you are familiar with PHP, its the same idea.
echo is like using
print command in other programming languages.
hope this helps, check out my other tutorials for beginners