If you happened to came to Linux world, then you must know that shell is considered one of the biggest assets, especially for those who loves automation and also scripting for their daily tasks.
But for some people, learning all those commands (including their parameter/options) will take some time and patience that not everyone has.
There's an easy way to solve that issue. Just visit explainshell.com and place your command there and they will try to explain it to you in detail. Well, basically they just grab the relevant part from the manual page, but it's easier for us to see instead of digging the whole manual pages. They already prepared some examples for you to try out.
All about my activities, thoughts, inspiration, ideas, and everything that happened around me
Showing posts with label Shells. Show all posts
Showing posts with label Shells. Show all posts
Sunday, December 08, 2013
Wednesday, February 16, 2011
Displaying Decimal Number in Shell Scripts
Have you ever wanted to display a decimal number on your shell scripts? Bash by default is not able to display any decimal number and only plays well with integers.
There's a solution for this, which is by using bc, a simple calculator which is mostly provided in any Linux distributions. Let's play a bit with bc.
In order to display a decimal number using bc, you will have to define a scale variable. Please have a look on bc's manual page for detail. Use this command on your terminal to try it: echo "scale=4; 3/4" | bc -l. It will give you an output of .7500
Here's a simple script for a demo:
Try running it:
./average.sh
Enter your score [0-100] ('q' for quit): 1
Enter your score [0-100] ('q' for quit): 5
Enter your score [0-100] ('q' for quit): 4
Enter your score [0-100] ('q' for quit): q
Average rating: 3.3333
There's a solution for this, which is by using bc, a simple calculator which is mostly provided in any Linux distributions. Let's play a bit with bc.
In order to display a decimal number using bc, you will have to define a scale variable. Please have a look on bc's manual page for detail. Use this command on your terminal to try it: echo "scale=4; 3/4" | bc -l. It will give you an output of .7500
Here's a simple script for a demo:
#!/bin/bash
SCORE=0
AVERAGE=0
SUM=0
NUM=0
while true; do
echo -n "Enter your score [0-100] ('q' for quit): ";
read SCORE;
if (("$SCORE" < "0")) || (("$SCORE" > "100")); then
echo "Be serious. Come'on, try again: "
elif [ "$SCORE" == "q" ]; then
echo -e "Average rating: \c"
result=`echo "scale=4; $SUM/$NUM" | bc -l`
echo "$result"
break
else
SUM=$[$SUM + $SCORE]
NUM=$[$NUM + 1]
fi
done
Try running it:
./average.sh
Enter your score [0-100] ('q' for quit): 1
Enter your score [0-100] ('q' for quit): 5
Enter your score [0-100] ('q' for quit): 4
Enter your score [0-100] ('q' for quit): q
Average rating: 3.3333
Tuesday, February 24, 2009
Bash 4.0
Bash 4.0 has been released and it encompasses lots of new things, described in the announcement.
The most notable new features are associative arrays, improvements to the programmable completion functionality, case-modifying word expansions, co-processes, support for the `**' special glob pattern, and additions to the shell syntax and redirections. The shell has been changed to be more rigorous about parsing commands inside command substitutions, fixing one piece of Posix non-compliance.I personally used bash 3.2 in Slackware-Current and it's still on /testing because it caused some script to break when we used a backtick on it. I wonder if bash 4.0 will have a solution for this and being used as the default shell in the next Slackware release.
Saturday, April 12, 2008
Using -Rf Option in Linux Shell
Linux shells can be powerful, but it can also be deadly, if you are using it without caution. The second one just applied to me. I usually deletes files using * wildcard to make things easier, since i don't want to delete one by one. Sometimes, i used -f parameter to delete without haste, but today, i got a bad experience using -f.
At first, i wanted to delete a movie file that i have downloaded using rm -f blabla.part*, but i accidentally wrote it like this rm -f blabla.part1.rar *. You know what happened. All of my data in that directory is gone
The lucky thing is that i didn't use -Rf option. If that's the case, i'll be surely screaming loudly. Most of the data that were gone were movies (no big deal, since i can re-download them again) and also some pictures (well, i can make those avatars again in short time), so nothing important data is lost. Thank God
Small tips : Make sure the syntax you are writing is correct, mostly when you use -Rf option in Linux shell.
At first, i wanted to delete a movie file that i have downloaded using rm -f blabla.part*, but i accidentally wrote it like this rm -f blabla.part1.rar *. You know what happened. All of my data in that directory is gone
The lucky thing is that i didn't use -Rf option. If that's the case, i'll be surely screaming loudly. Most of the data that were gone were movies (no big deal, since i can re-download them again) and also some pictures (well, i can make those avatars again in short time), so nothing important data is lost. Thank God
Small tips : Make sure the syntax you are writing is correct, mostly when you use -Rf option in Linux shell.
Subscribe to:
Posts (Atom)