<h1>Shell Scripting Be Like… Sweet but Stubborn </h1>
When I started learning shell scripting, people kept saying it was powerful. They said you can use it to make your computer do work for you. Like cleaning files or running tasks over and over without touching anything. That part is true. But learning it was not smooth for me at all.
One day I tried to write a script that counts numbers using a while loop. I wanted it to print from one to five. This was what I wrote first
`i=1
while [ $i -le 5 ]
do
echo "Counting: $i"
((i++))
done`
But it did not work the way I thought. I expected it to print one to five but it got confused. I later understood that even if ((i++)) looks simple, it behaves strange sometimes. So I changed it like this
`i=1
while [ $i -le 5 ]
do
echo "Number: $i"
i=$((i + 1))
done`
Now it worked. The loop printed every number just as I wanted. That felt good. But I was still making small mistakes that cost me a lot.
After some more practice I noticed something. Shell scripting is not too hard but it is very picky. Very very picky.
If you forget to put quotes around your variables and they have spaces the whole thing breaks
If you forget to add space between if and [ it will fail and not tell you why
If you ask the user for input using read and you do not save it in a variable the script will just sit there doing nothing
The biggest thing I learned was to take my time. Rushing only leads to silly errors. And the terminal does not forgive. It just shows errors or even worse it does nothing and you will sit there confused.
<h3> My Eviction Test </h3>
During the eviction test they gave us a task. It looked simple at first but I got stuck because I did not take time to breathe and think. The task was this
1 Ask the user to enter a directory name
2 Ask again for a file name
3 Create the directory
4 Create the file inside that directory
5 List the content of the directory
I wrote this
`read -p "Enter directory name: "
read -p "Enter file name: "
mkdir $dirname
touch
$dirname/$filename
ls $dirname`
But nothing worked. The script ran but no folder no file. I got confused and felt like I failed.
Later I realized the mistake. I did not store the input. I asked the user to type but I did not put the value inside a variable. So $dirname and $filename were empty.
Here is the fixed version
`read -p "Enter directory name: " dirname
read -p "Enter file name: " filename
mkdir "$dirname"
touch "$dirname/$filename"
ls "$dirname"`
Now the script worked. It created the folder. It created the file inside it. And it listed the content. The whole thing felt so easy after I fixed it. But in the exam I was just too nervous.
<h3>What I Learned from All This</h3>
Shell scripting is like giving your computer instructions. But if you whisper or skip one word the computer will not understand. So you have to be clear every time.
These are the things I always remember now
Always put your variables inside quotes
Always check that there is a space after if and before [
Always store your input when using read
Always take your time to write and think
Do not panic when it breaks. Fix it slowly
Mistakes do not mean you are bad. They mean you are learning. I still make mistakes now. But I understand better. And that makes me proud of how far I have come.