How to write a simple shell script and how to use Variables in shell script
To write a simple shell script and how to use Variables in the shell script
A variable is a character string to which we relegate an esteem. The esteem doled out could be a number, content, filename, gadget, or some other kind of information.
Let’s First Create a Simple shell script to print a statement
[root@linuxhelp11 ~]# vim simple.sh
#!/bin/bash
echo " This is shell script "
Now make the file as executable format as follows
[root@linuxhelp11 ~]# chmod +x simple.sh
Run the shell script using the “sh” and see the output
[root@linuxhelp11 ~]# sh simple.sh
This is shell script
Now let’s see another example for creating a Text file using the script Open a File and type the command to create a text file
[root@linuxhelp11 ~]# vim simple.sh
#!/bin/bash
touch /opt/newfile.txt
Verify the folder before execting the script
[root@linuxhelp11 ~]# ls /opt
Rh
Now lets execute the script and verify
[root@linuxhelp11 ~]# sh simple.sh
[root@linuxhelp11 ~]# ls /opt/
newfile.txt rh
A File has been created using a script
VARIABLES
A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables.
Syntax:
VARIABLE_NAME=" value "
> Variables are case sensitive
> VariableName must be in uppercase
Let’s see an example Create a file named variable.sh and add a variable as follows
[root@linuxhelp11 ~]# vim variable.sh
#!/bin/bash
VAR=script
echo "Shell ${VAR}ing is Interesting"
Now make the file executable and verify
[root@linuxhelp11 ~]# chmod +x variable.sh
[root@linuxhelp11 ~]# sh variable.sh
Shell scripting is Interesting
So as you see the Variable has been printed as an output after the exection And if you want to remove variable but not syntax just use the following script and print without variable
[root@linuxhelp11 ~]# vim variable.sh
#!/bin/bash
VAR=script
echo "Shell $VARing is Interesting"
[root@linuxhelp11 ~]# sh variable.sh
Shell is Interesting
Comments ( 0 )
No comments available