[Solved] Need help understanding ksh scripting.

Can any one please tell me where the error in below script. Output is not as per my requirement. if condition not comparing the result of record count of a file with 1. where is the pbm in my script? Appreciate your help..

#!/bin/ksh
#Module-
#Summary- To move the file to direcotries if condition satisified or failed
#Author-
set -x
Dir1="/home/s117013"
Dir2="/home/s117013/data"
Dir3="/home/s117013/tmp"
cd $Dir1
filename=test_3.sh
i=`gawk '{print $NR}' test_3.sh | wc -l`
if ($i > 1);
then
cp $Dir1/$filename $Dir3/$filename`date +%Y%m%d%H%M%S`.csv
else
cp $Dir1/$filename $Dir2/$filename_`date +%Y%m%d%H%M%S`.csv
fi

output:

+ sh test_print.sh
+ Dir1=/home/s117013
+ Dir2=/home/s117013/data
+ Dir3=/home/s117013/tmp
+ cd /home/s117013
+ filename=test_3.sh
++ gawk '{print $NR}' test_3.sh
++ wc -l
+ i=21
+ 21
test_print.sh: line 12: 21: command not found
++ date +%Y%m%d%H%M%S
+ cp /home/s117013/test_3.sh /home/s117013/data/20140212130431.csv

Is this a homework assignment?

If so, it needs to be posted in the Homework & Coursework Questions forum and the complete template for items posted in that forum must be filled out.

Welcome shivadanam,

I would suggest that if ($i > 1); is invalid syntax for a ksh script. You seem to have a more C like syntax. Try:-

if [ $i -gt 1 ]
then
   cp ..........

I hope that this helps,
Robin
Liverpool/Blackburn
Uk

Another ksh alternative would be
if (($i > 1)) or even if ((i > 1)) . That trailing semi-colon is harmless but useless. I actually like to do:

if ((i > 1)) ; then

which shortens a script while being easy to read.

Thanks for this. I learn more each day. I've never some across these formats of conditions, but I will not panic now if I do.

Robin

Korn shell allows integer operations inside double brackets. You can use these instead of the rather old-fashioned "let":

typeset -i x=0
(( x += 1 ))     # same as "let x=x+1"
print - $x

The same goes for integer comparisons, which is what Perderabo used. Note, that you need at least one space for the double brackets:

(( x += 1 ))     # correct
((x += 1))       # wrong

and you do NOT need to use "$" to denote variables.

I hope this helps.

bakunin

No spaces needed with double parentheses.

$
$ x=3
$ ((x+=1))
$ echo $x
4
$

Leaving off the $ is much faster and, provided that the variable only contains a simple scalar will give the same result.

Both ((expression)) and $((expression)) perform an arithmetic evaluation of expression. After evaluating expression, $((expression)) returns a string representing the resulting value. ((expression)) returns a 0 exit status if expression evaluates to a non-zero value and 1 otherwise.

Other than the time taken to format the results, there shouldn't be any noticeable difference in speed between ((expression)) and $((expression)) .

Note that the standards require conforming shells to support arithmetic expansion ( $((expression)) ). The standards explicitly allow for the arithmetic evaluation command ( ((expression)) ) to be provided, but do not currently require conforming shells to provide it. The standards also explicitly require applications that invoke nested subshells to separate the opening parentheses opening the subshell environments with one or more IFS characters to avoid an ambiguity in the grammar in shells that support the arithmetic evaluation command.

I mean ((x+=1)) is much faster than (($x+=1))

And that is dependent on the code being in a loop. ksh precompiles loops and executes the compiled code. With the second form, ksh must expand the variable and then rescan the full resulting expression. With the first form ksh can use the compiled code with no rescan.

Hi Robin,
I have changed the syntax as you menitoned and i tried.
Now it is working fine.
if [ $i -gt 1 ];

Thank you so much for your help... :smiley:

Thanks
Shiva