This will perform one of two blocks of code, depending on the condition of the first. Essentially is a bash terniary operator.
To tell if a machine is up:
ping -c1 machine { echo succes;} || { echo failed; }
Because of the bash { } block operators, you can have multiple commands
ping -c1 machine && { echo success;log-timestamp.sh }|| { echo failed; email-admin.sh; }
Tips:
Remember, the { } operators are treated by bash as a reserved word: as such, they need a space on either side.
If you have a command that can fail at the end of the true block, consider ending said block with 'false' to prevent accidental execution
Show Sample Output
Very simple web server listening on port 80 will serve index.html file or whatever file you like pointing your browser at http://your-IP-address/index.html for example. If your web server is down for maintenance and you'd like to inform your visitors about it, quickly and easily, you just have to put into the index.html file the right HTML code and you are done! Of course you need to be root to run the command using port 80.
Not as taxing on the CPU.
Really bored during class so I made this... Basically, you hold period (or whatever) and hit enter after a second and you need to make the next line of periods the same length as the previous line... My record was 5 lines of the same length. It's best if you do it one handed with your pointer on period and ring on enter.
It is the best way i found to send a mail from the console in my centos server.
If you have a client that connects to a server via plain text protocol such as HTTP or FTP, with this command you can monitor the messages that the client sends to the server. Application level text stream will be dumped on the command line as well as saved in a file called proxy.txt. You have to change 8080 to the local port where you want your client to connect to. Change also 192.168.0.1 to the IP address of the destination server and 80 to the port of the destination server. Then simply point your client to localhost 8080 (or whatever you changed it to). The traffic will be redirected to host 192.168.0.1 on port 80 (or whatever you changed them to). Any requests from the client to the server will be dumped on the console as well as in the file "proxy.txt". Unfortunately the responses from the server will not be dumped. Show Sample Output
A bit different from some of the other submissions. Has bold and uses all c printable characters. Change the bs=value to speed up and increase the sizes of the bold and non-bold strings.
This version works across on all POSIX compliant shell variants.
This is an example of the usage of pdfnup (you can find it in the 'pdfjam' package). With this command you can save ink/toner and paper (and thus trees!) when you print a pdf. This tools are very configurable, and you can make also 2x2, 3x2, 2x3 layouts, and more (the limit is your fantasy and the resolution of the printer :-) You must have installed pdfjam, pdflatex, and the LaTeX pdfpages package in your box. Show Sample Output
Same as above but slooooow it down
combination of several of the above
Generates labyrinth-like pattern on UTF-8 terminal in bash. For fun ;) Show Sample Output
(In French) Connection aux hotspots FreeWifi, et maintien de la connection active
This takes a picture (with the web cam) every 5 minutes, and send the picture to your e-mail. Some systems support mail -a "References: " so that all video surveillance emails are grouped in a single email thread. To keep your inbox clean, it is still possible to filter and move to trash video surveillance emails (and restore these emails only if you really get robbed!) For instance with Gmail, emails sent to me+trash@gmail.com can be filtered with "Matches: DeliveredTo:me+trash@gmail.com" Show Sample Output
In this case it runs the command 'curl localhost:3000/site/sha' waiting the amount of time in sleep, ie: 1 second between runs, appending each run to the console. This works well for any command where the output is less than your line width This is unlike watch, because watch always clears the display. Show Sample Output
it's nice to be able to use the command `ls program.{h,c,cpp}`. This expands to `ls program.h program.c program.cpp`. Note: This is a text expansion, not a shell wildcard type expansion that looks at matching file names to calculate the expansion. More details at http://www.linuxjournal.com/content/bash-brace-expansion I often run multiple commands (like apt-get) one after the other with different subcommands. Just for fun this wraps the whole thing into a single line that uses brace expansion.
`while true`: do forever `nc -l -p 4300 -c 'echo hello'`: this is the but anything can go here really `test $? -gt 0 && break`: this checks the return code for ctrl^c or the like and quite the loop, otherwise in order to kill the loop you'd have to get the parent process id and kill it. Show Sample Output
You can do some boolean logic like
A or B then C else D using
or : ||
and : &&
So you can do some :
# false || false && echo true || echo false
false
# true || false && echo true || echo false
true
# false || true && echo true || echo false
true
# true || true && echo true || echo false
true
and so on ...
I use it like :
(ssh example.com 'test something') || $(ssh example.net 'test something') && echo ok || echo ko
Show Sample Output
I use this loop for a variety of things. If a process won't die, I try to ask it nicely to let it die gracefully, and then i use killall and kill -9 to end its life. This will run the program killall and then start it again when it completes, indefinately.
This will crop each page of the PDF by 10mm left, 11cm bottom, 22pts right, and nothing from the top.
It takes the first value of /prov/loadavg to print that many stars followed by the value. Show Sample Output
The shell has perfectly adequate pattern matching for simple expressions. Show Sample Output
This command produces the output of "du -sk testfile" in every 10 seconds. You can change the command to be whatever you want.
shell loop to scan netstat output avoiding loolback aliases (local/remote swap for local connections) Show Sample Output
I wrote a script called bootstrap.py to delete the database, then load a new database with initial values. With this single-line shell loop, when I need to make a schema change (which happens often in the early stages of some projects), I hit ctrl-C to stop the running Django server, then watch bootstrap.py do its thing, then watch the server restart. Show Sample Output
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.
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
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: