[Solved] Need help understanding ksh scripting.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Need help understanding ksh scripting.
# 1  
Old 02-12-2014
[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..

Code:
#!/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:
Code:
+ 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

Moderator's Comments:
Mod Comment Moved from Bits forum to UNIX for Dummies Questions and Answers Forum.

CODE tags added.

Last edited by Don Cragun; 02-12-2014 at 05:00 AM.. Reason: Add CODE tags; move to appropriate forum.
# 2  
Old 02-12-2014
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.
# 3  
Old 02-12-2014
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:-
Code:
if [ $i -gt 1 ]
then
   cp ..........


I hope that this helps,
Robin
Liverpool/Blackburn
Uk
# 4  
Old 02-12-2014
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:
Code:
if ((i > 1)) ; then

which shortens a script while being easy to read.
This User Gave Thanks to Perderabo For This Post:
# 5  
Old 02-12-2014
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
# 6  
Old 02-12-2014
Quote:
Originally Posted by 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":

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

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

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

I hope this helps.

bakunin
# 7  
Old 02-12-2014
No spaces needed with double parentheses.
Code:
$
$ 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.
This User Gave Thanks to Perderabo For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trouble understanding shell scripting (mostly wsname)

Am still learning Scripting and I come across a build command that I don't really understand if /local/bin/wsname 2>/dev/null; then base="`/local/bin/wsname`" export base fi if ; then /local/bin/wsname exit 1 fi WSNAME="$base"/ can some one in light me to what... (1 Reply)
Discussion started by: Wpgn
1 Replies

2. Shell Programming and Scripting

[Solved] Help understanding this code!!

Hi guys, I am still learning awk and much apprecated to shed some light on the following: the questions asked is below! { total = i = 0 do { ++i total += $i } while ( total <= 100 ) print i, ":", total } File used: cat test.do 45 25 60 20 10 105 50 40 33 5 9 67 108 3 5 4 (2 Replies)
Discussion started by: Apollo
2 Replies

3. Shell Programming and Scripting

[Solved] Shell scripting

I am new to write scripts. I want help to write a shell script to create 10 users and passwords to users. Can any please help me. (9 Replies)
Discussion started by: DONFOX
9 Replies

4. Shell Programming and Scripting

[Solved] KSH: Array/If Help

RedHat 5 KSH I am creating an array, and then using case to go through and count for specific words. Then the count gets stored as an expression. string='ftp rcp rsh telnet ftp ftp' set -A myarray $string FTPCOUNT="0" for command in ${myarray} do case $command in ftp) FTPCOUNT=`expr... (2 Replies)
Discussion started by: nitrobass24
2 Replies

5. Shell Programming and Scripting

[Solved] Help with Sort command (ksh)

hello everyone, I have file with de-limited values in random order shown below, where C1V0 represents column1 value1, C2V0 represents column2 value 2, and so on. Column0 is a constant value which i didn't represent in the sample input format. Column1 is a numeric column. Column2 is a... (0 Replies)
Discussion started by: angie1234
0 Replies

6. Shell Programming and Scripting

[Solved] AIX ksh script -d <- what does it mean?

I'm looking at a line in a script: && DRROOT="/dir1" || DRROOT="/dir2" I'm trying to understand it. Is there a "-d" command and is it seeing if /dir1 exists and, if so, set an environment variable named DRROOT to "/dir1", else set DRROOT to "/dir2" ? Thanks, -dog ---------- Post... (0 Replies)
Discussion started by: landog
0 Replies

7. Shell Programming and Scripting

Understanding Shell Scripting

Hi Gurus, Im new to Shell scripting. I have a shell script which basically sends an email when called thorugh my ETL tool. Wanted to understand the its functionality in detail. Would be great it any one can explain what exactly the commands to #!/bin/sh # Dummy UUCP rmail command for... (1 Reply)
Discussion started by: r_t_1601
1 Replies

8. Shell Programming and Scripting

[Solved] need help in shell scripting

Hi Friends, Please help me with the following problem: I have a text file with the following lines: Dated: 8/11/2011 <br> Time : 0000 <br> ============== <br> --------------------------------------<br>Data Upload Time =4:51:52... (2 Replies)
Discussion started by: anuajay1988
2 Replies

9. Shell Programming and Scripting

Understanding of a script as a scripting newbie

Hi! I have the following script and do not understand part of it. I have a very little understanding of scripting. The script is for Nagios to check the response of fast-com.de. The guy who has written it is no longer in the company. #!/bin/sh PATH=/adm/bin:/bin:/usr/bin export PATH ... (2 Replies)
Discussion started by: locutus01
2 Replies
Login or Register to Ask a Question