declare -a array[2]
for (( i=0; i<3; i++ ))
do
read array[$i]
done
for (( n=0; n<3; n++ ))
do
echo -e "From Table[$n]: ${array[$n]}\n " ;
done
sqlplus owner/pass#db << ENDOFSQL
#file.sql
exit;
ENDOFSQL;
I Need to use the bash array[] variables in the file.sql script.
If you want to create file.sql from the input, you don't need an array. You can read and write in the same for loop
for (( i = 0; i < 3; i++ )); do
read a
echo "From Table[$i]: $a"
done >file.sql
this will create file.sql, which you can then pass to sqlplus
sqlplus owner/pass#db #file.sql
You should also be able to skip the input file altogether and pipe the output of the for loop directly to sqlplus
for (( i = 0; i < 3; i++ )); do
read a
echo "From Table[$i]: $a"
done | sqlplus owner/pass#db
Related
Say, I have a C code which I compile like:
$ gcc code.c -o f.out
$ ./f.out inputfile outputfile
Then the code asks for input
$ enter mass:
Now if I need to run this code for example 200 times and the input files have name : 0c.txt, 1c.txt, ....., 199c.txt etc and I want to use same value of mass every time (e.g. mass=6) then how do I write an "awk" command for that? Thanks for your help.
You don't specify your outputfile name. I'll assume 0c.out, 1c.out, ...
I'm also assuming that the f.out program reads the mass from stdin instead of anything more complicated.
#!/usr/bin/gawk -f
BEGIN {
mass = 6
for (i=0; i<200; i++) {
cmd = sprintf("./f.out %dc.txt %dc.out", i, i)
print mass |& cmd
close(cmd, "to")
while ((cmd |& getline out) > 0) {
do something with each line of output from ./f.out
}
close(cmd)
}
}
ref http://www.gnu.org/software/gawk/manual/html_node/Two_002dway-I_002fO.html
In bash, you'd write:
for i in $(seq 0 199); do
echo 6 | ./f.out ${i}c.txt ${i}c.out
done
I am new user of gawk. I am trying to read trace file by putting a small code in a file and then by making that file executable. Following is what I am trying to do.
#!/bin/sh
set i = 0
while ($i < 5)
awk 'int($2)=='$i' && $1=="r" && $4==0 {pkt += $6} END {print '$i'":"pkt}' out.tr
set i = `expr $i + 1`
end
after this I am running following command:
sh ./test.sh
and it says:
syntax error: word unexpected (expecting do)
any help?
Assuming you are using bash
Syntax of while loop:
while test-commands; do consequent-commands; done
more info
For comparison using < operator you need to use Double-Parentheses see Shell Arithmetic and Conditional Constructs.
To assign value to the variable you used in the code just write i=0.
To access a shell variable in awk use -v option of awk.
Thus your might be become like this:
i=0
while ((i < 5))
do
awk -v k=$i 'int($2)==k && $1=="r" && $4==0 {pkt += $6} END {print k":"pkt}' out.tr
i=`expr $i + 1`
done
Here the variable k in awk code, has the value of variable $i from shell.
Instead of expr $i + 1 you can use $((i + 1)) or shorter $((++i))
Also you can use for loop then your code becomes much cleaner:
for (( i=0; i < 5; i++ ))
do
awk -v k=$i 'int($2)==k && $1=="r" && $4==0 {pkt += $6} END {print k":"pkt}' out.tr
done
I've found some related posts, but nothing seems to work.
I want to repeat the same argument $i for the instances 03-12. I'm really trying to use some nco operators - but the printf statement is hanging me up.
#!/bin/csh
set i = 1
while ($i < 2)
`printf O3_BDBP_1979ghg.cam.h0.00{03,04,05,06,07,08,09,10,11,12}-%02d.nc $i`
# i = $i + 1
end
The output is - so it gets it for 03 but not the rest.
printf: O3_BDBP_1979ghg.cam.h0.0004-%02d.nc: expected a numeric value
I've also tried this statement (per other posts)
`printf O3_BDBP_1979ghg.cam.h0.00{03,04,05,06,07,08,09,10,11,12}-%1$02d.nc $i`
Any suggestions would be greatly appreciated!
The braces produce multiple arguments for the printf command; only the first is treated as a format string, while the rest are treated as arguments for %1 in the first. In other words, you're getting
printf O3_BDBP_1979ghg.cam.h0.0003-%02d.nc O3_BDBP_1979ghg.cam.h0.0004-%02d.nc ... O3_BDBP_1979ghg.cam.h0.0012-%02d.nc $i
as the effective command line. Try a nested loop instead:
#!/bin/csh
set i = 1
while ($i < 2)
foreach j ( {03,04,05,06,07,08,09,10,11,12} )
printf O3_BDBP_1979ghg.cam.h0.00%02-%02d.nc $j $i
end
# i = $i + 1
end
I read this stackoverflow question...
Bash: check user input is correct
which does most of what I want however rather then checking it's just an integer I need to check it's an integer in a variable range....
The script looks for files in a directory and then assigns a number to them...
File 1
File 2
File 3
etc....
The user chooses the the number and the script then executes commands against that file.....the variable $FILELIST is the total number of files.
Taking the example from the previous stackoverflow I tried.....
FILENUM=""
while [[ ! ($FILENUM =~ ^[0-$FILELIST]+$) ]]; do
echo " "
echo "Please enter the file number: "
read -p "1 - $FILELIST" FILENUM < /dev/tty
done
echo "$FILENUM"
However this is throwing a syntax error: unexpected "(" (expecting "do") in the while line and I'm not sure why, I suspect $FILELIST has to be bracketed somehow but an explanation as to why the above works would help me understand the problem.
Thanks
bash-specific answers:
You don't need to reinvent the wheel: use the select builtin:
cd /path/to/directory
PS3="Select a file: "
select file in *; do
if [[ $file ]]; then break; fi
done
echo "You selected '$file'"
echo "You selected file number $REPLY"
To check a number is within a certain range, I'd write:
if (( 0 <= $number && $number <= $max )); then echo "in range"; fi
Since you're using ash you might use this as a reference: http://manpages.debian.net/cgi-bin/man.cgi?query=dash
while true; do
FILENUM=""
echo
echo "Please enter the file number: "
read -p "1 - $FILELIST" FILENUM < /dev/tty
if expr "$FILENUM" : '[0-9]\+$' &&
[ $FILENUM -gt 0 ] &&
[ $FILENUM -le $FILELIST ]
then
break
fi
done
echo "$FILENUM"
How do you print an integer into a string in Maya MEL scripting?
It turns out you can just use + to concatenate string and integer objects in Maya Embedded Language.
For example:
int $i ;
string $s ;
for( $i = 0; $i < 5; $i++ ) {
$s = "ooh" + $i ;
print $s ;
}
There is also the format command