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?

1
  • Here read this Commented Aug 30, 2019 at 11:45

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

And if I want to pass the path to directory it'll be the same way as well?
Yes, it will be the same way.
$0 is not a positional parameter; it's a special parameter. N starts at 1.
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

2 Comments

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, 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, you will probably need them sooner or later.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.