I have a linux script that generates a string and prints it to the console. I want this string to be the name of a file and open it for editing in vi. How can this be done?

link|improve this question

74% accept rate
feedback

3 Answers

up vote 2 down vote accepted

You can try with:

vi -- "$(your-script)"
link|improve this answer
feedback

Use the backtick char to escape the script: (on the same key as the tilde ~ char)

vi `./script`

And if the output has spaces that you stil want to see as a single parameter:

vi "`./script`"

test it:

vi "`echo This is my new filename.txt`"

link|improve this answer
feedback

Does your script do this?

var=somestring
echo $var

That prints simplestring on the console

But this...

var=somestring
date >$var

will create a file named somestring and put the date and time into it. But maybe you want to be really clever...

echo -n What file should I use for the log?
read var
exec >$var 2>&1
date

The -n tells echo not to emit a newline. The exec command doesn't execute anything, just changes the stdout (#1) and stderr (#2). The 2>&1 says to make 2 (stderr) go to the same place as 1 (stdout).

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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