Results 1 to 3 of 3
I have a text file that contains a single word and I want to write a bash script that will read the word from the text file... The following is ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 06-10-2011 #1
- Join Date
- Dec 2010
reading a value from a textfile using bash script (beginner problem)
I have a text file that contains a single word and I want to write a bash script that will read the word from the text file... The following is my incorrect attempt, as it assigns the name of the textfile to the variable as opposed to the word stored within the textfile:
(assume I have a text file value.txt that has as its contents a single word, say wordone)
for f in value.txt
so the output of the above script is value.txt, however I want it to be wordone.
to summarise: how do I assign the value of the word contained within a textfile to a variable?
- 06-11-2011 #2
- Join Date
- Jan 2010
Yes, for-operator in BASH expects a list of string-values.
Hence, "value.txt" is treated as the first and the only value in such list.
Below I put some changes (in unified diff format -- it's easy to understand):
#!/bin/sh -for f in value.txt +for f in $(cat value.txt) do echo $f done
Last edited by saulius2; 06-11-2011 at 06:01 AM.
- 06-11-2011 #3