Pablo Fonseca
Pablo Fonseca

Reputation: 69

How to run a shell script with multiple statements in a single command line?

I am trying to run a program using multiples interval as arguments. I am using a for loop in a shell script to do this. However, I only was capable of running this code step by step, as showed bellow. I would like to know if there is a way to run this command in a single command line. I just begin to learn the shell lenguage. I already tried some combineation of commands such as do and then, but withou sucess. Any kind of help is welcome.

Thank you,

for K in 1 $(seq 1 5000000 160000000)
    > do
    > J=$((K+5000000))
    > impute2 -phase -m ../impute2_chr1.map -g ../CHR1_17_04_17.gz -int $K $J -Ne 20000 -o phasing_CHR1_${K}_${J}
    > done

Upvotes: 2

Views: 1304

Answers (3)

Mark Setchell
Mark Setchell

Reputation: 207385

Create a file called go in your HOME directory that looks like this:

#!/bin/bash
for K in 1 $(seq 1 5000000 160000000); do
  J=$((K+5000000))
  impute2 -phase -m ../impute2_chr1.map -g ../CHR1_17_04_17.gz -int $K $J -Ne 20000 -o phasing_CHR1_${K}_${J}
done

Now make that file executable (just do it one time) with:

chmod +x $HOME/go

Now you can run it as many times as you like by typing:

$HOME/go

If you want students to be able to run it, store it in /usr/local/bin/go and change $HOME to /usr/local/bin throughout.

Upvotes: 0

Lizardx
Lizardx

Reputation: 1195

for K in 1 $(seq 1 5000000 160000000); do J=$((K+5000000)); impute2 -phase -m ../impute2_chr1.map -g ../CHR1_17_04_17.gz -int $K $J -Ne 20000 -o phasing_CHR1_${K}_${J};done

I find 'do' not requiring its own ; always a bit counter-intuitive, but otherwise it's easy to understand. 'then' and 'else' follow that same logic, by the way, as in:

if [[ 'x' == 'y' ]];then echo 'yes'; else echo 'no'; fi

To satisfy a comment: you use ; to replace the line breaks. With the exception of the 'do', 'else', 'then', as noted. When in doubt, create a simpler form of the thing, see where the ; should be used to have it work, then do the more complicated form.

Upvotes: 2

PetitCassoulet
PetitCassoulet

Reputation: 25

In bash, different instructions are separated by either newlines \n or ;, but structuring your code on multiple lines makes it way easier to read and grasp faster, so I'd suggest leaving it on multiple lines except if you have specific needs but I can't think of any right now.

Upvotes: 0

Related Questions