Question:
Unix Shell scripting - the easiest FOR loop that doesn't work?
hatch coltraine
2009-10-12 08:03:44 UTC
Here's what I need. I need to echo incremental lines to a sql script that sets flags. Here's an example

script current:

create table TABLE{
f1 varchar2,
f2 varchar2,
.
.
.
fN varchar2
}

I'm trying to write a FOR loop that will echo additional lines to the end of the file, so it looks like this:

create table TABLE{
f1 varchar2,
f2 varchar2,
.
.
.
fN varchar2
PF1 varchar2
PF2 varchar2
.
.
.
PFN varchar2
}

assume that I need to add hundreds of flags

Here's my loop:

i=1
while [[$i <= $1]] ;
do
echo "PF_"$i "VARCHAR2(2 BYTE))," >> $2
i=i+1
done
}

$1 = the number of flags the user wants, and $2 is the file that they want to append to

I get this:

A file or directory in the path name does not exist.
make_FLAGS.ksh[8]: =: 0403-016 Cannot find or open the file.


and no output at all. I'm testing this in the directory where $2 resides, and the chmod is all good.

Any ideas?

Thanks!
Three answers:
techieguy
2009-10-12 08:46:10 UTC
Try passing the second parameter enclosed in double quotes, and/or the full path. Ensure that you have read and write permissions on the SQL script.



Also, your shell script needs to be tweaked a bit. Unfortunately I do not have Korn shell, but this is how it would work in Bash.



===================================================

$

$ # contents of the SQL script

$ cat myfile.sql

create table TABLE (

f1 varchar2(2 byte),

f2 varchar2(2 byte),

f3 varchar2(2 byte),

f4 varchar2(2 byte),

f5 varchar2(2 byte),

f6 varchar2(2 byte)

);

$

$ # contents of the Bash shell script

$ cat testscr.sh

#!/usr/bin/bash

i=1

while [ "$i" -le "$1" ]

do

echo " PF_"$i" VARCHAR2(2 BYTE)," >> $2

i=`expr $i + 1`

done



$

$ # run the Bash shell script

$ ./testscr.sh 10 "myfile.sql"

$

$ # now check the contents of the SQL script

$ cat myfile.sql

create table TABLE (

f1 varchar2(2 byte),

f2 varchar2(2 byte),

f3 varchar2(2 byte),

f4 varchar2(2 byte),

f5 varchar2(2 byte),

f6 varchar2(2 byte)

);

PF_1 VARCHAR2(2 BYTE),

PF_2 VARCHAR2(2 BYTE),

PF_3 VARCHAR2(2 BYTE),

PF_4 VARCHAR2(2 BYTE),

PF_5 VARCHAR2(2 BYTE),

PF_6 VARCHAR2(2 BYTE),

PF_7 VARCHAR2(2 BYTE),

PF_8 VARCHAR2(2 BYTE),

PF_9 VARCHAR2(2 BYTE),

PF_10 VARCHAR2(2 BYTE),

$

$

===================================================



HTH,

techieguy
2016-05-22 12:44:38 UTC
If perl is installed you can do something like this perl -e 'print sprintf("%0.7d","1234");' Here is a perl script that does most of the work. #!/usr/bin/perl my $file = $ARGV[0]; my $tmpfile = $file.$$; open(IN,$file); open(OUT,">$tmpfile"); while($line = ) { chomp($line); # removes linefeed # Assuming tab seperated columns my ($col1, $col2, $col3) = split(/\t/,$line); # Assuming the first $col1 is the column for the numbers. print OUT sprintf("%0.7d\t%s\t%s",$col1, $col2, $col3), "\n"; } close(OUT); close(IN); # You can then copy the $tmpfile to the original filename $file
C-Ray
2009-10-12 08:11:49 UTC
Hey dude, check out the following links:

----------------------------------------------------------

1. http://partmaps.org/era/unix/shell.html

2. http://www.codewalkers.com/c/a/Miscellaneous/Loops-in-the-UNIX-Shell/

3. http://www.unix.com/unix-dummies-questions-answers/66142-unix-loops.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...