While loop with input in a bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop with input in a bash script
# 1  
Old 10-09-2015
While loop with input in a bash script

I have the following while loop that I put in a script, demo.sh:
Code:
while read rna; do
      aawork=$(echo "${rna}" | sed -n -e 's/\(...\)\1 /gp' | sed -f rna.sed)
      echo "$aawork" | sed 's/ //g'
      echo "$aawork" | tr ' ' '\012' | sort | sed '/^$/d' | uniq -c | sed 's/[ ]*\([0-9]*\) \(.*\)/\2: \
\1/'

This is how I use it in a terminal:
Code:
./demo.sh < input.txt

Now I need to make this bash code (the above while loop) part of another script, master.sh, but I am stuck as how to pass the input file to it.

I appreciate your help.

Last edited by faizlo; 10-09-2015 at 08:28 PM..
# 2  
Old 10-09-2015
Without seeing your other script (and how you invoke it), any guess that we could make would likely be wrong.

But, if nothing else in your other script reads from its standard input, sourcing demo.sh in your other script or physically including the text of demo.sh it in your other script should work.
# 3  
Old 10-10-2015
bash, tr, awk, sed

This is my master.sh script:
Code:
#!/bin/bash                                                                                             
#check to see if there is an input file:                                                                
if [ "$#" -lt 1 ]
then
  echo "Usage: $0 input_file ..."
  exit 1
fi

#Check if the file is empty or not                                                                      
file=$1
if [[ -s $1 ]]                   # if not empty:
then
  echo ""
  echo "**** $file has data."
  cat $1 | sed 's/>/\n>/g' > temp1.txt
  cat temp1.txt | sed '/^>/ d' > temp2.txt
  awk '!NF{if(++n <=1) print; next}; {n=0; print}' < temp2.txt > dna.out
  awk '/./{printf "%s",$0;next} {print "\n";} END{if (/./)print""}' dna.out > DNA.out
  echo "**** Number of DNA sequences:"
  grep -cve '^\s*$' DNA.out
  echo "**** Mumber of empty lines:"
  grep -ce '^\s*$' DNA.out
  echo ""
  #Remove duplicate empty lines:                                                                        
  awk '!NF{if(++n <=1) print; next}; {n=0; print}' < temp2.txt > DNA.out                               
  echo "**** DNA.out created (removed FASTA header)"
  #convert to mRNA and remove temp1, temp2 to avoid confusion                                           
  tr ACGT ACGU < DNA.out > RNA.out
  echo "**** RNA.out created (T -> U)"
  echo ""
  rm temp1.txt temp2.txt
  moved to conversion:                                                                                
  while read rna; do                                                                                  
      aawork=$(echo "${rna}" | sed -n -e 's/\(...\)\1 /gp' | sed -f rna.sed)                          
      echo "$aawork" | sed 's/ //g'                                                                   
      echo "$aawork" | tr ' ' '\012' | sort | sed '/^$/d' | uniq -c | sed 's/[ ]*\([0-9]*\) \(.*\)/\2\
: \1/'                                                                                                  
  done                                                                                                
  echo "**** creating AminoAcid from DNA:"
  ./convert_dna.sh < DNA.out > DNA_AminoAcid.out
  echo "**** Amino Acids from DNA saved in DAN_AminoAcid.out."
  echo "**** creating AminoAcid from RNA:"
  ./convert_rna.sh < RNA.out > RNA_AminoAcid.out
  echo "**** Amino Acids from RNA saved in RAN_AminoAcid.out."
  echo ""

else    # if the file is empty.                                                                         
  echo "**** $1 has no data, or file does not exist."
  echo "**** done!"
  echo ""
#clean the FASTA file and remove all lines starting with >, adding a new line first to avoid mixing all DNA samples:                                                                                           
fi;

convert_dna.sh and convert_rna.sh are two scripts that contain the while loop I mentioned in my first post. One with dna.sed and the other with rna.sed.

My question is how to add the while loop two times one with the dna.sed and the other one with rna.sed.
# 4  
Old 10-10-2015
Use redirection: while ...; do ...; done < input_file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Input redirection within bash script

Hi, when I try to redirect input and the command is described as a string within an array redirection does not work. why? #!/bin/bash dir=("tail < ./hello.txt") tail < ./hello.txt #works ${dir} #does not work (2 Replies)
Discussion started by: heinzel
2 Replies

2. Shell Programming and Scripting

Help in input file's in bash script

hello guys i have bash script to open my routers with username and password i made script but i have problem this script can/t read password from file #!/bin/bash router_file="ips" passwd="password.txt" for router in cat ;$router_file do for pass in cat ;$passwd; do res=$(curl -m 1 ... (7 Replies)
Discussion started by: manhoud
7 Replies

3. Shell Programming and Scripting

Bash shell script with mandatory and optional input

Hello I would like to write a bash shell script which will need user to supply one variable which is mandatory and some other optional variables. If mandatory variable is not supplied by user, the script will exit. If optional values are not supplied by user, hard-coded value (in the script)... (3 Replies)
Discussion started by: atanubanerji
3 Replies

4. Shell Programming and Scripting

Bash loop hording keypress input

I have a bash loop that waits for a single key press, then does $something depending on what $key is pressed before refreshing the screen with updated data. The problem I have is that the script will store additional key presses and chain them together causing the screen to redraw and the script... (1 Reply)
Discussion started by: DarkPhoenix
1 Replies

5. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

6. UNIX for Dummies Questions & Answers

Feed Input to a script running on bash

Hi Sir, I am just learning bash scripting and I came across a challenge. I need to input F11 to a script among many text inputs. For all the text inputs i did following. # sh test.sh < input.txt where input.txt contains all the text inputs in new lines. This worked fine until i... (1 Reply)
Discussion started by: gaurav kumar
1 Replies

7. Shell Programming and Scripting

Detail on For loop for multiple file input and bash variable usage

Dear mentors, I just need little explanation regarding for loop to give input to awk script for file in `ls *.txt |sort -t"_" -k2n,2`; do awk script $file done which sorts file in order, and will input one after another file in order to awk script suppose if I have to input 2 or... (4 Replies)
Discussion started by: Akshay Hegde
4 Replies

8. Shell Programming and Scripting

Bash Script for parse input like option and value

I would create a bash script than parse like this: test.sh -p (protocol) -i (address) -d (directory) I need retrive the value after -p for example... understand??? I hope... thanks (6 Replies)
Discussion started by: ionral
6 Replies

9. Shell Programming and Scripting

Input file to bash script

Hi, I have this script Script.sh: #!/bin/sh sed 's,\,,g' input.dat > output .dat But i want to run it witb different files. So i want the input file as an input argument to the script, how could i do that. Running it like this: > Script.sh input.dat (2 Replies)
Discussion started by: Johanni
2 Replies

10. UNIX for Dummies Questions & Answers

Writing a loop to process multiple input files by a shell script

I have multiple input files that I want to manipulate using a shell script. The files are called 250.1 through 250.1000 but I only want the script to manipulate 250.300 through 250.1000. Before I was using the following script to manipulate the text files: for i in 250.*; do || awk... (4 Replies)
Discussion started by: evelibertine
4 Replies
Login or Register to Ask a Question