Undefined "n" value and simplifying script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Undefined "n" value and simplifying script
# 1  
Old 02-12-2011
Undefined "n" value and simplifying script

I have a very 'dirty' bash script that is working and looks like this:
Code:
#!/bin/bash
for i in {1..n}
do
 mv block${i} infile
 ./dnadist <<-EOF
 D
 Y
 EOF
 rm infile
 mv outfile Result${i}
 mv Result${i} infile
 ./neighbor <<-EOF
 Y
 EOF
 rm infile
 rm outfile
 mv outtree Result${i}
done

Most of the times I do not know the value for "n", how can I modify the script in such way that even if I do not know the number of files in that particular folder I will be still able to process them?
Other little thing, I am pretty sure there should be a way to 'clean' the script to make it look "cleaner".
Any help will be greatly appreciate it!
# 2  
Old 02-12-2011
Can you also explain what you're trying to achieve?
There are probably better approaches to do what you want.
# 3  
Old 02-12-2011
Calculating matrix distances and generating a tree

Franklin52,
Let me try to explain what I am trying to accomplish. I have hundreds of files named "block1", "block2", etc; containing DNA sequences in Phylip format. I want to calculate the genetic distance for each "block" file using the binary program "dnadist" which uses D (Kimura) and Y (YES) as options. Unfortunately, "dnadist" won't let me use anything as an input but a file named "infile". Thus, I start my script by renaming the files from "blockX" to infile and then feed it into dnadist. The output file that is generated by "dnadist" is bydefault "outfile" and I cannot change the file name either. I just came to realize that I do not need to "temporarily" rename the "outfile" to "ResultX" but instead to "infile", therefore I can delete that part. Finally, I grab the "new" "infile" and process it using "neighbor", using Y (YES) as option, in order to generate the corresponding trees. "neighbor" generates two different files "outfile" and "outtree", I only need the "outtree" file, which I rename as "ResultX", and that' why I just need to delete the "outfile". Then, we process the next file "block2" and the cycle starts again.
As I said, many times I do not know the number of "block" files in my folder, and therefore I would like to modify my script to be able to process whatever number of files, it can range from some few to several hundreds, are present in that given folder.
So, the my script now looks something like this:
Code:
#!/bin/bash
for i in {1..2}
do
 mv block${i} infile
 ./dnadist <<-EOF
 D
 Y
 EOF
 rm infile
 mv outfile infile
 ./neighbor <<-EOF
 Y
 EOF
 rm infile
 rm outfile
 mv outtree Result${i}
done



Thanks!

Last edited by Xterra; 02-12-2011 at 11:41 AM..
# 4  
Old 02-12-2011
Can't you do something like:
Code:
n=$(ls block* | wc -l)

while [ $n -ge 1 ]
do
 mv block${i} infile
 ./dnadist <<-EOF
 D
 Y
 EOF
 mv outfile infile
 ./neighbor <<-EOF
 Y
 EOF
 rm outfile
 mv outtree Result${i}
 n=$(( $n - 1 ))
done


Last edited by Franklin52; 02-12-2011 at 11:57 AM.. Reason: adding code
# 5  
Old 02-12-2011
Franklin

I am not quite sure I understand how to modify my script following your suggestion.
You mean something like this?:
Code:
#!/bin/bash
n=$(ls block* | wc -l)
while [ $n -ge 1 ]
do
    mv block${n} infile
    ./dnadist <<-EOF
    D
    Y
    EOF
    rm infile
    mv outfile infile
    ./neighbor <<-EOF
    Y
    EOF
    rm infile
    rm outfile
    sed "s/:[0-9\.-]*//g" outtree > Result${n}
    rm outtree
    n=$(( $n - 1 ))
done

Thanks once again!
# 6  
Old 02-12-2011
Yes, something like that and the rm command is superfluous here:
Code:
rm infile
mv outfile infile

# 7  
Old 02-12-2011
Franklin

What I am trying to do in that particular step is to delete the original "infile" and replace it with the new "outfile".
Should I only leave it as:
Code:
#!/bin/bash
n=$(ls block* | wc -l)
while [ $n -ge 1 ]
do
 mv block${n} infile
 ./dnadist <<-EOF
 D
 Y
 EOF
 mv outfile infile
 ./neighbor <<-EOF
 Y
 EOF
 rm outfile
 mv outtree Results${n}.dis
 rm infile
 n=$(( $n - 1 ))
done
cat *.dis > intree2
rm *.dis
./treedist <<EOF
2
l
v
y
EOF


Last edited by Xterra; 02-12-2011 at 07:25 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Beginners Questions & Answers

Simplifying awk script using multiple "|"

I have the following script: awk -F "," '{ if ( $4 > 450 && $4 < 550 && $5 > 0.5 ) print $2, $5; else print $2, "0" }' test.txt | awk '{a+=$2}END{for(i in a){print i, a}}' | sort -nk 1.2 | sed 1,2d and a bunch of files that look like the test file attached here. I am outputting all... (2 Replies)
Discussion started by: Xterra
2 Replies

3. Programming

Compiling C++ code with NetCDF libraries: "undefined reference"

Hi! I am trying to compile a C++ code with cmake and gcc on Ubuntu. The code uses NetCDF4 libraries. I specify the path to these libraries as follows: -I/usr/local/include -L/usr/local/lib -lnetcdf -lnetcdf_c++4 "ccmake" and "cmake" work fine. After typing "make" I receive the error... (0 Replies)
Discussion started by: Alauda
0 Replies

4. Emergency UNIX and Linux Support

Perl error: Can't call method "value" on an undefined value

Hi, I am running a perl script to automate a process and I keep running into a error can't find the "value" Can't call method "value" on an undefined value at process_file.pl line 44. file is CVS cell is ifdfdxrfmp.ksh Here is the script I have also attached it as well: ... (2 Replies)
Discussion started by: vpundit
2 Replies

5. Solaris

I got "undefined reference to" on gcc

Hell all I tryed to build rmp from sources on Solaris 10. I download source code, install gcc, binutils, and other packs с sunfreeware.com. Doring compilation I got an error: /bin/bash ./libtool --tag=CC --mode=link gcc -std=gnu99 -g -O2 -fPIC -DPIC -D_REENTRANT -Wall... (0 Replies)
Discussion started by: sluge
0 Replies

6. Programming

make fails with "undefined reference to..."

i am compiling a program called vasp on suse and get the following error. there are many more preprocess and ifort commands prior so i just grabbed the tail of the log file: ./preprocess <main.F | /usr/bin/cpp -P -C -traditional >main.f90 -DMPI -DHOST=\"LinuxIFC\" -DIFC -Dkind8 -DNGZhalf... (6 Replies)
Discussion started by: crimso
6 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

bash: "undefined variable" and pipe

Hi, haven't found anything about this through searching, so may be a new topic: when doing this: set -o nounset set -o errexit find . -name "*.lib" | while read library; do echo ${libary} done echo "after while" I expect the script to exit within the while loop (because of nounset and... (6 Replies)
Discussion started by: nagaidhlig
6 Replies

9. AIX

Getting error "Undefined symbol: .u_strlen_2_6"

Hi, I am using xlC compiler. The compilation goes fine but at the time of linking it gives the following error ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information. ld: 0711-317 ERROR: Undefined symbol: .u_strlen_2_6 ld: 0711-317 ERROR: Undefined symbol:... (0 Replies)
Discussion started by: nachiketv
0 Replies

10. Programming

shared object "undefined symbol: fstat" error

Didn't have this problem in AIX, but ported to Linux with GCC compiler and am now getting a runtime error: tssutil: symbol lookup error: /work/agility/devel/bin/libagam.so: undefined symbol: fstat I'm sure most of you know that fstat is an intrinsic function just like printf, memcpy, etc. When I... (5 Replies)
Discussion started by: marcus121
5 Replies
Login or Register to Ask a Question