Commands tagged while loop (13)

  • This one-liner fills the screen with randomly colored lines.


    3
    while :; do printf "\e[48;2;$((RANDOM % 256));$((RANDOM % 256));$((RANDOM % 256))m%*s\e[0m" $(tput cols) ""; sleep 0.1; done
    wuseman1 · 2023-07-04 00:38:46 347
  • While going through the source code for the well known ps command, I read about some interesting things.. Namely, that there are a bunch of different fields that ps can try and enumerate for you. These are fields I was not able to find in the man pages, documentation, only in the source. Here is a longer function that goes through each of the formats recognized by the ps on your machine, executes it, and then prompts you whether you would like to add it or not. Adding it simply adds it to an array that is then printed when you ctrl-c or at the end of the function run. This lets you save your favorite ones and then see the command to put in your .bash_profile like mine at : http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html Note that I had to do the exec method below in order to pause with read. t () { local r l a P f=/tmp/ps c='command ps wwo pid:6,user:8,vsize:8,comm:20' IFS=' '; trap 'exec 66 exec 66 $f && command ps L | tr -s ' ' >&$f; while read -u66 l >&/dev/null; do a=${l/% */}; $c,$a k -${a//%/} -A; yn "Add $a" && P[$SECONDS]=$a; done } Show Sample Output


    2
    for p in `ps L|cut -d' ' -f1`;do echo -e "`tput clear;read -p$p -n1 p`";ps wwo pid:6,user:8,comm:10,$p kpid -A;done
    AskApache · 2010-10-12 06:42:10 7
  • `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


    1
    while true ; do nc -l -p 4300 -c 'echo hello'; test $? -gt 0 && break; done
    rawco · 2016-03-22 21:55:44 13
  • At times I find that I need to loop through a file where each value that I need to do something with is not on a separate line, but rather separated with a ":" or a ";". In this instance, I create a loop within which I define 'IFS' to be something other than a whitespace character. In this example, I iterate through a file which only has one line, and several fields separated with ":". The counter helps me define how many times I want to repeat the loop.


    0
    while [[ COUNTER -le 10 && IFS=':' ]]; do for LINE in $(cat /tmp/list); do some_command(s) $LINE; done; COUNTER=$((COUNTER+1)); done
    slashdot · 2010-09-01 15:09:59 4
  • If you're very busy and don't want to wait for a ping response, use it. This command will be waiting for a successful ping response, to play a sound file to warn you that the target host is available.


    0
    continuar=true; while $continuar; do if ping -c 3 [target_IP_address] 2>&1> /dev/null ; then mplayer [sound_file]; continuar=false; break; fi; done
    mack · 2011-04-25 21:44:05 18
  • pcspkr have to be enabled! modprobe pcspkr xset b on


    0
    ping -a IP-ADDRESS
    markussesser · 2011-04-28 13:51:12 23
  • For use when you can't use "watch" (user-defined functions, aliases). This isn't mine - its an alternate posted in the comments by flatcap, and is the shortest and easiest to remember.


    0
    while sleep 1; do foo; done
    lowbatteries · 2012-09-14 20:21:04 4
  • Simply add this to whatever apache startup script you have, or if you are on a MAC, create a new automator application. This will show a pretty growl notification whenever theres a new Apache error log entry. Useful for local development


    0
    /usr/bin/tail -fn0 /path/to/apache_error.log | while read line; do /usr/local/bin/growlnotify --title "Apache Notice" --message "$line"; done &
    jhyland87 · 2013-01-22 05:25:41 5
  • Sometimes I get FLAC files that RhythmBox can't play but VLC can. So I re-encode them using GStreamer at highest compression.


    0
    find . -type f -iname '*.flac' | while read i; do mv -- "$i" "$i.tmp"; gst-launch filesrc location="$i.tmp" ! flacdec ! flacenc quality=8 ! filesink location="${i%.tmp}"; rm -- "$i.tmp"; done
    qdrizh · 2014-07-10 19:21:22 7
  • This command will take the output of curl and read it line by line, skipping a step in downloading the file then parsing it. You can then parse each line, or only print the lines that contain certain works using if statements, or whatever you can come up with. Or you can change IFS and use it to parse based on separators other than newline.


    0
    while read line; do echo $line;done < <(curl -s <URL of file to read>)
    baize · 2016-02-05 17:04:15 18
  • Abort/Break with CTRL-C when no output is shown anymore (break while true loop). Show Sample Output


    0
    while true; do for bzipfile in $(file *|egrep bzip2|awk '{print $1'}|cut -d':' -f1); do bunzip2 $bzipfile; done; done
    tapestreamer · 2016-12-12 16:12:18 15
  • while commandt do command command ... done {commandt is executed and its exit status tested.} for i in 1 2 3 > do > echo $i > done Show Sample Output


    -3
    i=0; while [ $i -lt 100 ]; do echo "test, ttest, tttest-${i}" >> kk.file; i=`expr $i + 1`; done
    kaushalmehra · 2012-09-13 21:46:18 4
  • Consider the following simple situation [ reading something using while and read ] [See script 1 in sample output] --------------------------------------------------- The variable var is assigned with "nullll" at first. Inside the while loop [piped while] it is assigned with "whillleeee". [Onlly 2 assignments stmts]. Outside the loop the last assigned value for "var" [and no variable] inside the while can't be accessed [Due to pipe, var is executed in a sub shell]. In these type of situation variables can be accessed by modifying as follows. [See script 2 in sample output] ___________________________ Vary helpful when reading a set of items, say file names, stored on a file [or variable] to an array an use it later. Is there any other way 2 access variables inside and outside the loop ?? Show Sample Output


    -5
    while read line; do echo $line; done <<< "$var"
    totti · 2011-09-22 16:53:32 5

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


Check These Out

force unsupported i386 commands to work on amd64
The above was done using the i386 flashplayer plugin, and was installed on a AMD64 machine running an AMD64 kernel and AMD64 programs. the resulting plugin install ultimately didn't work for swiftfox (but worked for iceweasel) without also covering it with a nspluginwrapper which took a bit of fenangaling to get to work (lots of apt-getting) but it is a nice feature to be able to trick installers that think you need i386 into running on a amd64, or at least attempting to run on amd64. Enjoy

Gets the english pronunciation of a phrase
Usage examples: say hello say "hello world" say hello+world

Generat a Random MAC address
Generate a random MAC address with capital letters

Extract tarball from internet without local saving

Check whether laptop is running on battery or cable
The original proc file doesn't exist on my system.

Install pip with Proxy
Installs pip packages defining a proxy

Send a local file via email
This just reads in a local file and sends it via email. Works with text or binary. *Requires* local mail server.

dont execute command just add it to history as a comment, handy if your command is not "complete" yet

list files recursively by size

Get the IP address
gives u each configured IP in a seperate line.


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: