Peter
Peter

Reputation: 117

Giving parameters to bash via command line

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?

Upvotes: 1

Views: 69

Answers (3)

Walter A
Walter A

Reputation: 20002

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

Upvotes: 1

Punit Narang
Punit Narang

Reputation: 46

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.

Upvotes: 2

Alex Martian
Alex Martian

Reputation: 3782

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

Upvotes: 1

Related Questions