0

I need help in bash script:

Let's say I have a bash.sh file such as:

cat 
$1Viral_sequences_loci/Fasta_viral_loci_seq_* > $1Viral_sequences_loci/All_fasta_viral_loci.fna

and I run bash bash.sh /path1/path2/path3/

so I would replace $1 by /path1/path2/path3/:

cat 
/path1/path2/path3/Viral_sequences_loci/Fasta_viral_loci_seq_* > /path1/path2/path3/Viral_sequences_loci/All_fasta_viral_loci.fna

but what if I want to reduce only from one order in the path for instance:

cat 
/path1/path2/path3/Viral_sequences_loci/Fasta_viral_loci_seq_* > /path1/path2/Viral_sequences_loci/All_fasta_viral_loci.fna

where the second $1 is not /path1/path2/path3/ but /path1/path2/

1
  • If the second is completely different from the first, use two parameters instead of one. This is also the most flexible solution. If you always can derive the second from the first like in your example, use the approach suggested by Michal Commented Oct 29, 2019 at 16:36

1 Answer 1

3

"${1%/*/}/" removes the shortest possible suffix pattern matched by bash's * including the trailing / following the previous /, and then re-adds the /. With shopt -s extglob, ?(/) could be used for that trailing / to make it optional.

5
  • 4
    While this code may solve the problem the answer would be a lot better with an explanation on how/why it does. Remember that your answer is not just for the user that asked the question but also for all the other people that find it. Commented Oct 29, 2019 at 16:20
  • @NathanOliver yea i know but wanted to answer fast and then edit Commented Oct 29, 2019 at 16:21
  • 9
    Please don't try to be the fastest gun in the west. All you do is hurt the quality of the site while your answer is in a less than ideal state. Commented Oct 29, 2019 at 16:22
  • @chippycentra yeah i pasted the wrong thing. fixed. Commented Oct 29, 2019 at 17:05
  • @chippycentra yay, i got points — thanks for bothering to click accept Commented Oct 29, 2019 at 17:55

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.