Hey everyone!
I am having some trouble with a bash script. I am generating 4 variables through scraping with selenium and I want to create 4 different files, one for each variable.
Right now, I am printing the variables in a vertical list to the terminal.
I need the files to have static names e.g.: 1.txt for variable 1, 2.txt for variable 2, etc etc.
The content of the file should be:
string=$variable
http://127.0.0.1/$string
How can I do this?
What does the rest of your script look like?
3 Likes
I ended up doing it in the original python script.
My question still stands though, how can you run a different command for each item in an array?
for i in $array; do
# do whatever on $i
done
With stringvalues bash loops trough all words (seperated by whitespace/eol chars)
nx2l
June 26, 2021, 11:42pm
#5
Like the others… i dont think enough information has been provided yet for someone to help without making wild assumptions
2 Likes
If I understand you correctly I would simply do:
VAR1="if"
VAR2="something"
VAR3="else"
VAR4="nothing"
# DRY violation, but simple enough here
echo -e "string=VAR1\n\nhttp://127.0.0.1/$VAR1" >> 1.txt
echo -e "string=VAR2\n\nhttp://127.0.0.1/$VAR2" >> 2.txt
echo -e "string=VAR3\n\nhttp://127.0.0.1/$VAR3" >> 3.txt
echo -e "string=VAR4\n\nhttp://127.0.0.1/$VAR4" >> 4.txt
Why complicate matters by trying to be clever?
2 Likes