Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

create a temporary file in a command line call

Terminal - create a temporary file in a command line call
any_script.sh < <(some command)
2010-02-21 18:44:33
User: cb0
9
create a temporary file in a command line call

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.

Know a better way?

If you can do better, submit your command here.

What others think

I really would like to know why someone votes -1 for this ?

Comment by cb0 102 weeks and 4 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

Comment by unixhome 102 weeks and 4 days ago

Let me try to post the two commands again:

'time wc -l <

'time wc -l /etc/services'

Hope it works this time.

Comment by unixhome 102 weeks and 4 days ago

# time wc -l <

# time wc -l /etc/services

Comment by unixhome 102 weeks and 4 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.

Comment by cb0 102 weeks and 4 days ago

Hum... Why dont simply use stdin ? oO

echo "SELECT COUNT(*) FROM mysql.user" | mysql

COUNT(*)

9

Comment by sputnick 102 weeks and 4 days ago

For bash you should use

mysql <<< "SELECT COUNT(*) FROM mysql.user"
Comment by sputnick 102 weeks and 4 days ago

works on zsh as well

Comment by zabuch 102 weeks and 4 days ago

let's try again:

<(some command)

works on zsh as well

Comment by zabuch 102 weeks and 4 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.

Comment by tremby 102 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.

Comment by tremby 102 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.

Comment by cb0 102 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.

Comment by tremby 102 weeks and 3 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts