The shell script one-liner thread

Discussion in 'all things UNIX' started by Gullible Jones, Nov 15, 2014.

  1. Gullible Jones

    Gullible Jones Registered Member

    Joined:
    May 16, 2013
    Posts:
    1,461
    This thread is for "clever" one-liners that help you accomplish something useful.

    Example:

    Code:
    # Try to mount all attached storage volumes, and throw away any error messages
    sh -c "awk '{print \$4}' /proc/partitions | xargs -I DEV pmount /dev/DEV 2> /dev/null"
    
    # Try to unmount all attached volumes, throw out error messages
    sh -c "awk '{print \$4}' /proc/partitions | xargs -I DEV pumount /dev/DEV 2> /dev/null"
    Note that I'm using 'sh -c' here so that the command strings can be invoked by a program that won't necessarily spawn a shell; e.g. by a window manager, as a menu entry. I redirect STDERR to /dev/null so as not to pollute ~/.xsession-errors. Upshot is, I can now use a WM menu entry to mount (or unmount) all storage devices I've plugged in, bypassing the need for polkit/logind/udisks/gvfs/Thunar/etc; all that's needed is pmount.

    Edit: actually this might be better doable as a one-line awk script, using system(). The quotes keep messing me up on that though.

    Edit 2: the way to do it with awk is as follows:

    Code:
    # Mount
    awk '/[0-9]+/ {system("pmount " $4 " 2> /dev/null")} ' /proc/partitions
    
    # Unmount
    awk '/[0-9]+/ {system("pumount " $4 " 2> /dev/null")} ' /proc/partitions
    Remember that in awk, *spaces* are used for string concatenation!

    Edit 3: removed leading spaces from regex, they're not necessary and may mess it up. Also note that this still won't work in Fluxbox due to the embedded braces.
     
    Last edited: Nov 16, 2014
  2. Gullible Jones

    Gullible Jones Registered Member

    Joined:
    May 16, 2013
    Posts:
    1,461
    Okay, this is a five-liner, but what the heck:

    Code:
    #!/bin/sh -
    for host in $@; do
      dig $host | egrep '\sA\s' | awk '{print $5}' \
        | xargs -iADDR ufw allow out to ADDR
    done
    You feed it a list of domains as arguments, and it enables them in UFW. This way (assuming you've run 'ufw default reject outgoing' at some point), you can quickly build a whitelist of acceptable domains.

    Edit: fixed it to work with aliases as well.

    Edit 2: and similarly for deleting entries

    Code:
    #!/bin/sh -
    for host in $@; do
      dig $host | egrep '\sA\s' | awk '{print $5}' \
        | xargs -iADDR ufw delete allow out to ADDR
    done
    Edit 3: I'll see if I can come up with something later for pruning wrong/unused IPs from the list...
     
    Last edited: Dec 8, 2014
  3. Gullible Jones

    Gullible Jones Registered Member

    Joined:
    May 16, 2013
    Posts:
    1,461
    Using dmenu as a fast directory browser - in this case, opening the target directory in gnome-terminal.

    Code:
    sh -c 'find . -type d | dmenu -i | xargs -iDIR gnome-terminal --working-directory=DIR'
    I have this bound to Win-space.
     
  4. Gullible Jones

    Gullible Jones Registered Member

    Joined:
    May 16, 2013
    Posts:
    1,461
    Select a window in X, and instantly renice it from the default 0 to 1, reducing its CPU priority:

    Code:
    renice -n 1 $(xprop | awk '/PID/ {print $3}')
    Edit: don't need grep before awk. D'oh!
     
  5. Gullible Jones

    Gullible Jones Registered Member

    Joined:
    May 16, 2013
    Posts:
    1,461
    Similar but better on multicore machines: select the window and lock its program to the second CPU core.

    Code:
    taskset -a -pc 1 $(xprop | awk '/PID/ {print $3}')
    This seems to produce reasonable results wtih Qemu (both with and without KVM support). Can't say for Virtualbox yet.
     
  6. Linux38911

    Linux38911 Registered Member

    Joined:
    Feb 17, 2015
    Posts:
    9
    Location:
    Netherlands
    I got a oneliner:
    Code:
    lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB" " " $9 " " $1 }' | sort -n -u | tail
    Show the largest 10 currently open files, the size of those files in Megabytes, and the name of the process holding the file open.
     
  7. Linux38911

    Linux38911 Registered Member

    Joined:
    Feb 17, 2015
    Posts:
    9
    Location:
    Netherlands
    Quickly encrypt a file with openssl (assuming that openssl is present in most, if not all linux distro's):
    Code:
    openssl aes-256-cbc -a -salt -in secret.txt -out secret.txt.enc
    when you execute this command you will be prompted to enter a password.
    a nice way to quickly encrypt a file maybe. :shifty:
    Replace "secret.txt" and "secret.txt.enc" with your own names of choice
    To decrypt:
    Code:
    openssl aes-256-cbc -d -a -in secret.txt.enc -out secret.txt
     
    Last edited: Feb 21, 2015
  8. Gullible Jones

    Gullible Jones Registered Member

    Joined:
    May 16, 2013
    Posts:
    1,461
    For installing all the build dependencies of anything and everything on your Fedora (or CentOS, etc.) system:

    Code:
    rpm -qa | xargs -iX yum-builddep -y X
    Be warned, this will use up a lot of disk space. (But it's probably worth it!)
     
  9. x942

    x942 Guest


    I personally use gpg for this. You can do so with:

    Code:
    gpg --output doc.gpg --symmetric doc
    
    
     
Loading...