2

I am trying to write a bash script to a run a python program which take a files name and print values in the terminal.My bash program should take three argument from the terminal.First the python program name,second the folder name and third the file name where I want to store the output of my python program.

#!/bin/bash
directoryname = "$1"
programname = "$2"
newfilename ="$3"
for file in directoryname
 do
  python3 programname "$file"  >> newfilename
 done

and I am executing the program as follows: ./myscript.sh mypython.py /home/data myfile.txt

but it is giving error as :

 ./myscript.sh: line 2: directoryname: command not found
 ./myscript.sh: line 3: programname: command not found
 ./myscript.sh: line 4: newfilename: command not found

Please help me with this.I am pretty new to bash script.

3
  • 3
    Remove the spaces between the variable name and the = Commented Oct 28, 2015 at 1:33
  • You mean from the command line, right? You're saying "from the terminal", but to me that would imply prompting and reading a value from after the script has been run. Commented Oct 28, 2015 at 2:02
  • Are you wanting to process all the files in the named directory? If not, why do you have a loop? If so, you need to add /* (and $) — as in $directoryname/* — to the loop control. Commented Oct 28, 2015 at 2:03

2 Answers 2

3

Change to:

#!/bin/bash
directoryname="$1"
programname="$2"
newfilename="$3"
for file in $directoryname
do
      python3 "$programname" "$file"  >> "$newfilename"
done

No spaces around the =. A var is tagged with the $ before its name. In general, a var expansion is better if quoted ($var vs "$var").

And, I assume that you do want the list of files inside a directory, but the directoryname is only the directory itself (as /home/user/). If so, you will need:

for file in "$directoryname"/*
Sign up to request clarification or add additional context in comments.

Comments

0

The unnecessary spaces in-between = and the variable names and the values are causing the issues, remove the spaces and try again. Will work. :)

#!/bin/bash
directoryname="$1"
programname="$2"
newfilename="$3"
for file in directoryname
 do
  python3 programname "$file"  >> newfilename
 done

2 Comments

That's part of the problem, but not the whole problem.
Okay, i didn't know about the python shell command, but what i was trying to do was to eradicate the errors he mentioned. Thanks.

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.