# \[Solved\] Need help understanding ksh scripting.

**URL:** <https://community.unix.com/t/solved-need-help-understanding-ksh-scripting/339613>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [February 12, 2014, 2:37am UTC](https://community.unix.com/t/solved-need-help-understanding-ksh-scripting/339613 "2014-02-12T02:37:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![shivadanam](https://community.unix.com/letter_avatar/shivadanam/32/5_5575768a8748004e209b776fc1b2916d.png) [@shivadanam](https://community.unix.com/u/shivadanam)\
**Post date:** [February 12, 2014, 2:37am UTC](https://community.unix.com/t/solved-need-help-understanding-ksh-scripting/339613/1 "2014-02-12T02:37:06Z")

</div>

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..

```nohighlight
#!/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:

```nohighlight
+ 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

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [February 12, 2014, 3:34am UTC](https://community.unix.com/t/solved-need-help-understanding-ksh-scripting/339613/2 "2014-02-12T03:34:43Z")

</div>

Is this a homework assignment?

If so, it needs to be posted in the [Homework & Coursework Questions forum](http://www.unix.com/homework-coursework-questions/) and the complete template for items posted in that forum must be filled out.

---

<div class="post-metadata">

**Author:** ![rbatte1](https://community.unix.com/letter_avatar/rbatte1/32/5_5575768a8748004e209b776fc1b2916d.png) [@rbatte1](https://community.unix.com/u/rbatte1)\
**Post date:** [February 12, 2014, 6:50am UTC](https://community.unix.com/t/solved-need-help-understanding-ksh-scripting/339613/3 "2014-02-12T06:50:45Z")

</div>

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:-

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

```

I hope that this helps,  
Robin  
Liverpool/Blackburn  
Uk

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [February 12, 2014, 7:52am UTC](https://community.unix.com/t/solved-need-help-understanding-ksh-scripting/339613/4 "2014-02-12T07:52:31Z")

</div>

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:

```nohighlight
if ((i > 1)) ; then

```

which shortens a script while being easy to read.

---

<div class="post-metadata">

**Author:** ![rbatte1](https://community.unix.com/letter_avatar/rbatte1/32/5_5575768a8748004e209b776fc1b2916d.png) [@rbatte1](https://community.unix.com/u/rbatte1)\
**Post date:** [February 12, 2014, 8:09am UTC](https://community.unix.com/t/solved-need-help-understanding-ksh-scripting/339613/5 "2014-02-12T08:09:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/5_5575768a8748004e209b776fc1b2916d.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [February 12, 2014, 1:20pm UTC](https://community.unix.com/t/solved-need-help-understanding-ksh-scripting/339613/6 "2014-02-12T13:20:45Z")

</div>

> [@rbatte1](#):
>
> I've never some across these formats of conditions, but I will not panic now if I do.

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

```plaintext
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:

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

```

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

I hope this helps.

bakunin

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [February 12, 2014, 1:43pm UTC](https://community.unix.com/t/solved-need-help-understanding-ksh-scripting/339613/7 "2014-02-12T13:43:17Z")

</div>

No spaces needed with double parentheses.

```nohighlight
$
$ 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.

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [February 12, 2014, 3:00pm UTC](https://community.unix.com/t/solved-need-help-understanding-ksh-scripting/339613/8 "2014-02-12T15:00:01Z")

</div>

> [@perderabo](#):
>
> � � �
> 
> 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.

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [February 12, 2014, 3:37pm UTC](https://community.unix.com/t/solved-need-help-understanding-ksh-scripting/339613/9 "2014-02-12T15:37:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shivadanam](https://community.unix.com/letter_avatar/shivadanam/32/5_5575768a8748004e209b776fc1b2916d.png) [@shivadanam](https://community.unix.com/u/shivadanam)\
**Post date:** [February 13, 2014, 2:22am UTC](https://community.unix.com/t/solved-need-help-understanding-ksh-scripting/339613/10 "2014-02-13T02:22:49Z")

</div>

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... 😃

Thanks  
Shiva
