1

I have a bash script in which I want to run a a bunch of files of different times. Instead of creating a lot of if statements or creating a lot of bash scripts I was thinking if there's a way to accept which files to run in bash via command line.


#!/bin/bash

#generating training data

i_hard=0
i_soft=0
i_neutral=0

for entry in /home/noor/popGen/sweeps/slim_script/final/*
do
    if [[ $entry == *"hard_FIXED"* ]]; then
        echo "It's there!"
        /home/stuff/build/./test $entry > /home/noor/popGen/sweeps/msOut/final/hard_$i_hard.txt
        i_hard=$((i_hard+1))
    fi

    if [[ $entry == *"soft_FIXED"* ]]; then
        echo "It's there!"
        /home/stuff/build/./test $entry > /home/noor/popGen/sweeps/msOut/final/soft_$i_soft.txt
        i_soft=$((i_soft+1))
    fi
    if [[ $entry == *"neutral"* ]]; then
        echo "It's there!"
        /home/stuff/build/./test $entry > /home/noor/popGen/sweeps/msOut/final/neutral_$i_neutral.txt
        i_neutral=$((i_neutral+1))
    fi
done


What I want to do is:


#!/bin/bash


i=0

for entry in /home/final/*
do
    if [[ $entry == *parameter* ]]; then
        echo "It's there!"
        /home/stuff/build/./slim $entry > /home/final/parameter_$i.txt
        i=$((i+1))
    fi
done


So I want 'parameter' is what I want to give through command line which can be hard_FIXED, hard_0, and so on. How can I achieve that?

Peter
  • 97
  • 1
  • 9

3 Answers3

2

The Shell Script Parameters by default are assigned as:

$N

Here: N is Number starting from 0.

Also, $0 refers to script file itself or the Shell.

So, the parameters passed to Shell Script are available as:

$1, $2, $3 and so on.

For example:

./script.sh hard_FIXED

hard_FIXED will be available as $1.

So, inside the script you can capture them and use as needed.

Mihir Luthra
  • 6,059
  • 3
  • 14
  • 39
Punit Narang
  • 46
  • 1
  • 6
1

The first argument to bash from a command line can be found with the positional parameter $1, so if I got your intentions right:

#!/bin/bash

#generating training data

i_hard=0
i_soft=0
i_neutral=0

for entry in /home/noor/popGen/sweeps/slim_script/final/*
do
    if [[ $entry == $1 ]]; then
        echo "It's there!"
        /home/stuff/build/./test $entry > /home/noor/popGen/sweeps/msOut/final/hard_$i_hard.txt
        i_hard=$((i_hard+1))
    fi

    if [[ $entry == $1 ]]; then
        echo "It's there!"
        /home/stuff/build/./test $entry > /home/noor/popGen/sweeps/msOut/final/soft_$i_soft.txt
        i_soft=$((i_soft+1))
    fi
    if [[ $entry == $1 ]]; then
        echo "It's there!"
        /home/stuff/build/./test $entry > /home/noor/popGen/sweeps/msOut/final/neutral_$i_neutral.txt
        i_neutral=$((i_neutral+1))
    fi
done
Alex Martian
  • 3,423
  • 7
  • 36
  • 71
  • And if I want to pass path to directory, it'll be the same way as $1 for example and say $1/./test then? – Peter Aug 30 '19 at 11:42
  • @Peter, if you called your script like `./script.sh path/to/dir`, `$1` will contain `path/to/dir`. `$1/./test` would mean `path/to/dir/./test` which is equivalent to `path/to/dir/test`. Also read about [quotes in bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash), you will probably need them sooner or later. – Mihir Luthra Aug 30 '19 at 11:49
1

Look how the parameters are used here.

#!/bin/bash

# parameter 1 directory
# parameter 2 entry
# check number arguments
if (( $# != 2 )); then
   echo "Usage: $0 directory entry"
   exit 1
fi

# different way of testing, now check directory
test -d "$1" || { echo "$1 is not a directory"; exit 1; }

i=0

for entry in /home/final/*${2}*
do
   # No need for testing [[ $entry == *parameter* ]], this is part of the loop
   echo "${entry}: It's there!"
   /home/stuff/build/slim "${entry}" > /home/final/${2}_$i.txt
   # Alternative for i=$((i+1))
   ((i++))
done
Walter A
  • 19,067
  • 2
  • 23
  • 43