Bash is a powerful Linux terminal application and bash scripting can be used to automate repetitive tasks easily. In this post, we will look at some basic bash scripting.
Bash scripting
[pre class="brush:bash"]
vim temp.sh
#!/bin/bash
echo “Hello world…”
Sleep 1
echo “How are you?”
read ans
echo “you typed $ans”
chmod +x temp.sh
./temp.sh[/pre]
How to run a bash script from anywhere
[pre class="brush:bash"]
# create a directory say "bin" under your home directory.
# update your path variable to include this bin directory.
#Put this in .profile or .bash_profle file to make it permanent.
export PATH=$PATH":$HOME/bin"
#or use the following if you want the directory to precedent system directories
PATH=$HOME/bin:$PATH;
# create a script say, "hello" and keep it in your bin directory.
#Give execute permission to the hello script.
#!/bin/bash
echo My first program
# from any directory, you simply type:
$ hello
My first program[/pre]
# create a directory say "bin" under your home directory.
# update your path variable to include this bin directory.
#Put this in .profile or .bash_profle file to make it permanent.
export PATH=$PATH":$HOME/bin"
#or use the following if you want the directory to precedent system directories
PATH=$HOME/bin:$PATH;
# create a script say, "hello" and keep it in your bin directory.
#Give execute permission to the hello script.
#!/bin/bash
echo My first program
# from any directory, you simply type:
$ hello
My first program[/pre]
Comments
Post a Comment