create a temporary file in a command line call

any_script.sh < <(some command)
Sometimes you have a script that needs and inputfile for execution. If you don't want to create one because it may contain only one line you can use the ` mysql -uuser -ppass dbname < <(echo "SELECT * FROM database;") This can be very usefull when working with mysql as I showed in the example code above. This will create a temporary file that is used to execute mysql and for example select all entrys from a specific database.

9
By: cb0
2010-02-21 18:44:33

What Others Think

I really would like to know why someone votes -1 for this ?
cb0 · 494 weeks and 5 days ago
I have found that since this board is used by people with different skill levels, some may encounter difficulty with a certain command and quickly vote it down. Though I found your command to work, I also have to say that there is a huge speed penalty associated with it. Perhaps not the best application, but as a way to show our fellow commandlinefu readers and contributors that your command works, here is my example: time wc -l < 10675 real 0m0.497s user 0m0.001s sys 0m0.027s time wc -l /etc/services 10676 /etc/services real 0m0.006s user 0m0.003s sys 0m0.000s
unixhome · 494 weeks and 5 days ago
Let me try to post the two commands again: 'time wc -l < 'time wc -l /etc/services' Hope it works this time.
unixhome · 494 weeks and 5 days ago
# time wc -l < # time wc -l /etc/services
unixhome · 494 weeks and 5 days ago
yes I have to agree that there is a huge speed penatly when useing this command. But I also have to say that for bash scripts that use mysql this is the best method that worked for me without using the mysql shell. One could call it 'quick and dirty' but if you use it for bash scripts that are called every x hours, the speed penalty can be neglected I think.
cb0 · 494 weeks and 5 days ago
Hum... Why dont simply use stdin ? oO echo "SELECT COUNT(*) FROM mysql.user" | mysql COUNT(*) 9
sputnick · 494 weeks and 5 days ago
For bash you should use mysql <<< "SELECT COUNT(*) FROM mysql.user"
sputnick · 494 weeks and 5 days ago
works on zsh as well
zabuch · 494 weeks and 5 days ago
let's try again: <(some command) works on zsh as well
zabuch · 494 weeks and 5 days ago
Isn't this exactly the same as using a pipe? The some command | any_script.sh The process substitution is useful when you want to put some command's output into any_script.sh but any_script.sh won't take standard input for that particular function, only a filename. I've come across this a few times, I think with mplayer in one case. There's the similar >(some command) too, which provides a fifo to any_script.sh by filename and any_script.sh can thereby write to the input of some command.
tremby · 494 weeks and 4 days ago
For some reason a lot of what I just posted disappeared... Isn't this exactly the same as using a pipe? The <(some command) runs some command and connects its output to a fifo whose path is substituted into the command line, looking to any_script.sh as a filename. But then you're using the < to redirect the contents of the file (the fifo) to the standard input of any_script.sh. This is exactly the same as using a pipe like this: some command | any_script.sh The process substitution is useful when you want to put some command's output into any_script.sh but any_script.sh won't take standard input for that particular function, only a filename. I've come across this a few times, I think with mplayer in one case. There's the similar >(some command) too, which provides a fifo to any_script.sh by filename and any_script.sh can thereby write to the input of some command.
tremby · 494 weeks and 4 days ago
all of you are right, BUT you can do things with this syntax you can not do with pipes. ./script.sh -c <(echo -e "du -cs %d > /tmp/dusize\nls -al %d") -u <(echo -e "cb0\nadmin\nroot") -h "/home/%U" This sample script does need a command file, a userfile and a directory with placeholer. command file describes commands to excute one after another. userfile contains usernames on a line. Now the script reads in all those things and executes all commands in the command file in all diretorys whith all %U being replaced by usernames and alls %d being replaces by a diretory. Now you see this script takes 2 files as argument which are created in background. I thinks you can't do such things with pipes. Maybe with named pipes but I don't know much about them.
cb0 · 494 weeks and 4 days ago
By using this syntax you /are/ using named pipes. Read the "process substitution" section of the bash manual. You're right that you can't do the command in your last comment using normal pipes -- I am not disputing that. But my objection is in regard to the original command you posted. Let me explain again. The command you posted: any_script.sh < <(some command) The <(some command) gets substituted with the path to the fifo which is set up so you're left with something like any_script.sh < /dev/fd4 where /dev/fd4 is the fifo connected to the output of some command So /dev/fd4 is being redirected as *standard input* to any_script.sh -- its path is not being passed as an argument. You can do this with a normal pipe, like some command | any_script.sh The process substitution is only needed when you want the fifos which are set up to be passed as arguments to a command.
tremby · 494 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: