Shell Script Writing - Putting it all together
(Page 4 of 4 )
Now I will present a couple of concrete and whole scripts. This will be perfect for showing you how all this comes together. Of course, if you want to understand it you should have read all of my previous UNIX-related articles on this site, and by now made yourself familiar with the terminal.
I am not just talking about how to enter commands. I refer to having mastered writing files and scripts inside the terminal. If you have not, make sure to try out the VI or the Joe text editor.
The first one is simple. Add a given text n times into a file. The list of the arguments will be how I enumerated the text. First, we will create an add.sh text file. Inside this, we enter the text as follows:
#!/bin/bash
# This script will add argumentum 1 times
# inside argumentum 2 file
# the argumentum 3 text
if (( $# != 3 ))
then echo $# We need 3 parameter
exit 1;
fi
if ! [ -e "$2" ]
then
echo "File Does not exists"
exit 1;
fi
if ! [ -r "$2" ]
then
echo "File not readable"
exit 1;
fi
if ! [ -w "$2" ]
then
echo "File not writable"
exit 1;
fi
if [ $1 -lt 1 ]
then
echo we cannot write "$1" number of line
exit 1;
fi
list=$(seq 1 $1)
for i in $list
do
echo "$3" >> "$2" 2>>/dev/null
done
Now we can execute it with the chmod execute. After this, we need to enter on the terminal the following command:
./add.sh 5 alfa Avril
A good shell script should have a good treatment for invalid arguments and errors. Moreover, you should add a short comment to the start of each script that explains what it should do. This will help you clarify what you want to write and ensure that, if you open it a year later, you will understand it quite easily.
These steps are required for every script. Therefore, in the following example I will only offer the solution to an issue. It will be up to you to decide a name for the script and the rest of the stuff. The second task is to count the number of chars read from the terminal. For this, we will use the wc command counting the characters/lines/words depending on its argument.
#!/bin/bash
#count the number of chars read from the terminal
sum=0
while read line
do
number=$(echo $line | wc -c)
sum=$( expr $sum + $number)
done
echo "$sum";
The third task is to watch the appearance of a file. If the file is present, close.
# The purpose of this program is to watch until a specific file appears and
# as soon as this happens close
if [ $# -ne 1 ]
then
echo " Too few or to many parameters"
exit 1 # error
fi
echo Start searching
while [ ! -e "$1" ]
do
sleep 1
done
echo File is found
The fourth task is to repeat the argument of the script the number of times indicated by the input. We will check to see if the input is a number with the grep command. This, in essence, will try to find matching patterns in the input text. As an argument, it will get a regular expression that describes for what to search more generally than entering a concrete text. I will cover this more deeply during our next article, which will include the topics of pattern matching and regular expressions in the shell.
#!/bin/bash
#Repeat the input string the time of the down pushed number
#Exit on little x
if [ $# -ne 1 ]
then
echo Invalid number of arguments
exit 1
fi
while read -n 1 readchar
do
if [ "$readchar" == "x" ]
then
echo
exit 0
fi
if ! (echo "$readchar" | grep "^[0-9]$")
then
continue;
fi
echo
for (( i = 1; i<="$readchar"; ++i))
do
echo "$1"
done
done
In the end here, there is one more spot for which the starting comment will reveal its purpose:
#!/bin/bash
# Made by Gabor Bernat Fri Oct 31
# This script will count the number of files
# and directories under the input
# directory. If no argument present, use the working directory
old=$(pwd)
cd "$1" 2>> /dev/null #we will ignore the error messages
if [ $? == 0 ]
then
files=$(find -type f | wc -l)
dir=$(find -type d | wc -l)
echo Directory count: $dir
echo File count: $files
# so we managed to enter the directory
cd "$old" #re-enter the old directory
else
exit 1;
fi
This will be all for today. If you keep in mind that repetition is the mother of knowledge and is what sets apart an expert/master from a novice, you will realize in no time that you can do amazing things with the help of what I've shown you here. Nevertheless, there is more information you should know before you reach the status of an expert. So make sure you come back next week.
Thanks for reading my article. I ask that you also make the effort to rate my article. It will take just a couple of seconds. If you have any kind of question related to this tutorial or this domain I encourage you to use the blog following this article to express it. I will come back and answer you. Live With Passion!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |