0

I've been trying to write the following bash script

#!/bin/bash
python request.py $1 > output.txt
value=$(output.txt)
echo "$value"

The python code is to perform a google search and to store the urls to an output file. I wanted to print the txt file but I'm failing here.

Akshay Hiremath
  • 950
  • 2
  • 12
  • 34
  • 1
    Btw.: I suggest to replace `!/bin/bash` with `#!/bin/bash`. – Cyrus Nov 07 '21 at 09:20
  • Yes , I will do it ! – Amey Surya Nov 07 '21 at 09:24
  • When you use plain filenames like `request.py` or `output.txt` in a shell script (or Python for that matter), by default they refer to files in the directory the user was in when they ran the script, not necessarily the directory the script is in. See ["How can I get the source directory of a Bash script from within the script itself?"](https://stackoverflow.com/questions/59895) and [BashFAQ #28: "How do I determine the location of my script? I want to read some config files from the same place."](http://mywiki.wooledge.org/BashFAQ/028) – Gordon Davisson Nov 07 '21 at 18:01

1 Answers1

1

You can do this using cat

#!/bin/bash
python request.py $1 > output.txt
cat output.txt
Jetchisel
  • 7,493
  • 2
  • 19
  • 18
carlosdafield
  • 1,479
  • 5
  • 16
  • Is there a way to pass the output.txt as an argument to another python program?? – Amey Surya Nov 07 '21 at 09:23
  • `python myProgam.py output.txt` since you created it you can use it – carlosdafield Nov 07 '21 at 09:25
  • is there a way to store the content of the file in a variable for future usage ?? ```value=$(cat output.txt)``` is giving me the following error ```cat: output.txt: No such file or directory ``` Even thought the output.txt is accepted by python and is present in the same directory – Amey Surya Nov 07 '21 at 10:45
  • 2
    You are doing something wrong then; this code definitely creates a file with that name in the current directory. (Though a better solution is probably `output=$(python request.py "$1" | tee output.txt)`) – tripleee Nov 08 '21 at 11:15