Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to insert in my script a value (string) that I would read from a text file.

For example, instead of:

echo "Enter your name"
read name

I want to read a string from another text file so the interpreter should read the string from the file and not the user input.

share|improve this question
    
What does your text file look like? Is the entire file 1 variable or are they KEY=VALUE pairs? The solutions are quite different (if it's the latter, Takkat's answer applies, the former Radu's) –  minerz029 Oct 28 '13 at 11:37
    
Possible cross site duplicate of: stackoverflow.com/questions/7427262/… –  Ciro Santilli Mar 24 at 9:03
    
@CiroSantilli it is not crossposted if it was posted by a different user, there is no such thing as a "cross-site duplicate". The only thing that should be avoided is the same question asked by the same user on different sites. –  terdon Mar 24 at 17:52
add comment

4 Answers 4

#!/bin/bash

file="/path/to/filename" #the file where you keep your string name

name=$(cat $file)        #the output of 'cat $file' is assigned to the $name variable

echo $name               #test
share|improve this answer
    
I dont want to display the variable, I want that this variable should be read by the script for further processing. I have tried awk which reads line by line. –  user208413 Oct 28 '13 at 8:48
    
Ok, delete echo $name . I use it just for test. Next you can use $name variable for further processing anywhere you want in your script. –  Radu Rădeanu Oct 28 '13 at 8:51
    
I cant use it anywhere it my script because dhe script doesn't know the value of the variable. In our case the variable name. –  user208413 Oct 28 '13 at 9:28
3  
@user208413 The value of $name variable is the string from your file assigned using cat $file command. What is not clear or so difficult to understand? –  Radu Rădeanu Oct 28 '13 at 9:36
    
It's redundant to use a seperate $file variable if you're only using it once to load the $name variable, just use cat /path/to/file –  minerz029 Oct 28 '13 at 10:05
add comment

To read variables from a file we can use the source command or . operator

Lets assume the file contains the following line

MYVARIABLE="Any string"

we can then import this variable using

#!/bin/bash

source <filename>
echo $MYVARIABLE
share|improve this answer
2  
Whoever downvoted this answer has a problem. The answer is good and elegant, vote up! –  Radu Rădeanu Oct 28 '13 at 9:58
add comment

From within your script you can do this:

read name < file_containing _the_answer

You can even do this multiple times e.g. in a loop

while read LINE; do echo "$LINE"; done < file_containing_multiple_lines
share|improve this answer
add comment

One alternative way to do this would be to just redirect standard input to your file, where you have all the user input in the order it's expected by the program. For example, with the program (called script.sh)

#!/bin/bash
echo "Enter your name:"
read name
echo "...and now your age:"
read age

# example of how to use the values now stored in variables $name and $age
echo "Hello $name. You're $age years old, right?"

and the input file (called input.in)

Tomas
26

you could run this from the terminal in one of the following two ways:

$ cat input.in | ./script.sh
$ ./script.sh < input.in

and it would be equivalent to just running the script and entering the data manually - it would print the line "Hello Tomas. You're 26 years old, right?".

As Radu Rădeanu has already suggested, you could use cat inside your script to read the contents of a file into a avariable - in that case, you need each file to contain only one line, with only the value you want for that specific variable. In the above example, you'd split the input file into one with the name (say, name.in) and one with the age (say, age.in), and change the read name and read age lines to name=$(cat name.in) and age=$(cat age.in) respectively.

share|improve this answer
    
I have the following script: vh = awk "NR==3{print;exit}" /var/www/test/test.txt with this I read line 3 from text file named test.txt. Once i get a string I want to use this string as an input for creating a directory –  user208413 Oct 28 '13 at 10:18
    
Change that to vh=$(awk "NR==3 {print;exit}" /var/www/test.txt). You will then have the string saved in variable $vh, so you can use it like so: mkdir "/var/www/$vh" - this will create a folder with the specified name inside /var/www. –  Tomas Lycken Oct 28 '13 at 11:09
    
I have done this way but it still cant create the folder :( –  user208413 Oct 28 '13 at 11:34
    
@user208413: What error message(s) do you get? "It doesn't work" is, unfortunately, not a very helpful problem description... –  Tomas Lycken Oct 29 '13 at 11:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.