[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Shell script

Hi,

I'm putting a shell script together which basically goes through the directory "Genomes", and what I want it to do is: for any folder in the directory which contains a .psq file, use the file name to create a variable (lets call it $database).

for database in $(find ~/Genomes -name "*.psq")

once creating the variable, (here's an example: $Humanadenovirus1.psq) I then want to trim off the ".psq" from this variable, creating $Humanadenovirus1. I'm not sure on how to do this.

This variable will be used in a command line further down.

I then want to do the same thing again, but this time with a known variable, creating $antibodydb:

for antibodydb in $(find ~/DatabaseTesting -name "*ABDatabase.psq")

I then want to do the same again and trim the .psq from the $ABdatabase.psq variable. Again, I'm not sure how to trim .psq from this variable.

both variables above will then be used in the command line:

blastall -p blastp -m 8 -d $antibodydb -i $database

I'll attach my script so far as it will probably make more sense ;)

Thanks.

#!/bin/bash

for database in $(find ~/Genomes -name "*.psq")
#find how to trim .psq from $database
do  

  for antibodydb in $(find ~/DatabaseTesting -name "*ABDatabase.psq")
#find out how to trim .psq from $antibodydb   
do
    echo "Processing $database"
    blastall -p blastp -m 8 -d $antibodydb -i $database 
   done
 done

Open in new window

0
StephenMcGowan
Asked:
StephenMcGowan
  • 3
  • 2
  • 2
1 Solution
 
sentnerCommented:
Easy command to trim that is to use the "basename" command.  You can use that command to also strip the directory portion of the name.

find ~/Genomes -name "*.psq" | basename .psq

0
 
woolmilkporcCommented:
database=${database%.psq}

antibodydb=${antibodydb%.psq}
0
 
sentnerCommented:
Sorry, I piped that, but basename requires it as an argument... Instead, to do it with basename, you will have to do it like:

stripped_db=`basename $database .psq`

Alternatively, you can use sed with a regex substitution:

for database in $(find ~/Genomes -name "*.psq" | sed -e 's/.psq$//')
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
StephenMcGowanAuthor Commented:
Thanks,

Just tried to implement

database=${database%.psq}

antibodydb=${antibodydb%.psq}

in the following script, and received the following message:

newgenomevsantibody.sh: line 4: syntax error near unexpected token `database=${d                  atabase%.psq}'
newgenomevsantibody.sh: line 4: `database=${database%.psq}'



#!/bin/bash

for database in $(find ~/Genomes -name "*.psq")
database=${database%.psq}

do  

  for antibodydb in $(find ~/DatabaseTesting -name "*ABDatabase.psq")
antibodydb=${antibodydb%.psq} 
do
    echo "Processing $database"
    blastall -p blastp -m 8 -d $antibodydb -i $database 
   done
 done

Open in new window

0
 
woolmilkporcCommented:
Place it after 'do', not before!
0
 
StephenMcGowanAuthor Commented:
Cheers wool.

The two variables currently contain their full file extensions:

i.e. $database = /fs/nas15/home/mqbpgsm4/Genomes/Hepatitis/HepatitisC/genotype4/hepCgenotype4

is there a way for the variable to only contain the last directory?

i.e. from the path above:

$database = hepCgenotype4 (therefore removing all other directories?)

0
 
woolmilkporcCommented:
database=${database##*/}

Thx for the points!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now