0

I am trying to automate a python using bash which is as follows. I am using a loop in bash to provide a second argument to the python but up to no avail. I am getting index out of range for the python file. The code and error are below

#!/bin/bash
$input_file=$1
echo $1
for ((i = 2 ; i < 3 ; i++ )); 
do
    echo $i
    python extract_random_columns.py $input_file $i 

done  

Error:

./create_dataset.sh: line 2: =../data/ind_input_n_500_d_25_skyd_100.txt: No such file or directory
../data/ind_input_n_500_d_25_skyd_100.txt
2
2
Traceback (most recent call last):
  File "extract_random_columns.py", line 8, in <module>
    num_of_columns = int(argv[2])
IndexError: list index out of range
silent_dev
  • 1,566
  • 3
  • 20
  • 45
  • Because your assignment to `input_file` isn't valid syntax (the `$` isn't allowed), the variable is empty. – Charles Duffy Oct 21 '17 at 15:00
  • Then, because you aren't quoting `"$input_file"` when using it, the variable simply disappears rather than passing a single argument as an empty string. – Charles Duffy Oct 21 '17 at 15:01

1 Answers1

0
input_file="$1"
for ((i = 2 ; i < 3 ; i++ )); 
do
    echo $i
    python extract_random_columns.py "$input_file" "$i" 
done

remove $ from $input_file in first line

Rassoul
  • 174
  • 16
  • `"$input_file"` needs to be quoted much more than anything else here. `$i` is *mostly* safe unquoted because it's numeric (though "mostly" safe is not "entirely" safe -- if you have `IFS=1` and `i=1`, then `echo $i` will emit a blank line, whereas `echo "$i"` will properly emit 1). – Charles Duffy Oct 21 '17 at 14:59
  • ...that is to say: Make it `echo "$i"`, and `python extract_random_columns.py "$input_file" "$i"`. Without that done, a filename can be expanded into any number of separate arguments if it contains spaces, or any result of splitting it into strings parses as a glob. – Charles Duffy Oct 21 '17 at 15:03