[Ilugc] some shell script samples
- From: lug@xxxxxxxxxxxxxxxxxx (Chandrashekar Babu)
- Date: Wed, 26 Dec 2012 10:29:25 +0530
Hi,
$ cat sample.sh
#!/bin/ksh
[snip]
????square=`expr $n \* $n`
On KSH and Bash, you can as well use
square=$((n * n))
and avoid the overhead of expr command. The above construct
is called arithmetic expansion. At the shell prompt, you could
also do something like:
$ echo $(( (100000 + (100000 * 0.1236)) - (100000 * .103) ))
for basic math, without rushing to your handy calculator.
Basic looping is provided by the pleasant for() construct which is
very different from the C for.
Not really. There's a 'for loop' with C-like syntax on KSH93 and Bash:
for ((i=0; i<10; i++))
{
echo $i
}
And this works well for 'counting' for loops.
$ looping
for f in `ls /etc/*`
Yet another useless use of 'ls' command. You could've used
for f in /etc/*
Please note that I use backticks '`" which is the same key as ~.
[snip]
t='date'
t="date"
are both useless. ;)
t=`date`
You must avoid using back-tick (`) for command substitution
on all POSIX compliant shells - it lacks readability and cannot
be nested. Use $(command) instead.
You might also want to brush-up on basics of shell scripting
and keep a good tutorial/reference handy before your next post
on the topic :-)
Cheers,
--
Chandrashekar Babu.
http://www.chandrashekar.info/
Other related posts: