[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Cool shell scripts
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 81
Thread images: 6
File: optimizing-shell-scripts.jpg (34 KB, 310x240) Image search: [Google]
optimizing-shell-scripts.jpg
34 KB, 310x240
This thread is for sharing cool shell scripts you've written with everyone!
>>
>>53580808
My first shell script
yt(){ youtube-dl $1 --prefer-ffmpeg --get-title -i  | toilet -f term -F border && mpv $1 --no-video --loop --really-quiet }
>>
A SHA-256 renamer.

if [[ -n $1 ]]; then
for f in "$2"*."$1"; do
sum=$(sha256sum "$f" | awk '{print $1}')
mv -n -- "$f" "$2$sum.$1"
done
else
echo "a filetype is needed"
fi


Parameter 1 accepts a file format (jpg,png,gif) and Parameter 2 accepts the folder argument.
>>
>>53580808
grep across many files
if [ $2 ]; then
find . ! -path . -iname "$1" -print0 | xargs -0 grep -E --color -i "$2"
else
echo "Usage: [pattern] [expression]"
exit 1
fi
>>
#Execute the command at the "$1"th history line, as shown by the command.
#The command at that line is brought back up in history
function hse() {
line=$(history | egrep "^\W?$1")
if [ -n "$line" ]; then
command=$(echo $line | sed "s/[[:digit:]]* //")
eval $command
history -s $command #Record this command in history
else
echo "No History Line $1"
fi
}
>>
There some really cool looking scripts here. I hope /g/ can post more like so far.
>>
File: steve-ballmer-tongue-540x334.jpg (36 KB, 540x334) Image search: [Google]
steve-ballmer-tongue-540x334.jpg
36 KB, 540x334
bump
>>
gradients() {
f=3 b=4
for j in f b; do
for i in {0..7}; do
printf -v $j$i %b "\e[${!j}${i}m"
done
done
rst=$'\e[0m'
bld=$'\e[1m'
cat << EOF

$f0█ $bld█$rst $f1█ $bld█$rst $f2█ $bld█$rst $f3█ $bld█$rst $f4█ $bld█$rst $f5█ $bld█$rst $f6█ $bld█$rst $f7█ $bld█$rst
$f0█ $bld█$rst $f1█ $bld█$rst $f2█ $bld█$rst $f3█ $bld█$rst $f4█ $bld█$rst $f5█ $bld█$rst $f6█ $bld█$rst $f7█ $bld█$rst
$f0▓ $bld█$rst $f1▓ $bld█$rst $f2▓ $bld█$rst $f3▓ $bld█$rst $f4▓ $bld█$rst $f5▓ $bld█$rst $f6▓ $bld█$rst $f7▓ $bld█$rst
$f0▒ $bld█$rst $f1▒ $bld█$rst $f2▒ $bld█$rst $f3▒ $bld█$rst $f4▒ $bld█$rst $f5▒ $bld█$rst $f6▒ $bld█$rst $f7▒ $bld█$rst
$f0░ $bld█$rst $f1░ $bld█$rst $f2░ $bld█$rst $f3░ $bld█$rst $f4░ $bld█$rst $f5░ $bld█$rst $f6░ $bld█$rst $f7░ $bld█$rst
$rst
EOF
}
>>
install-gentoo() {
str="${@:-Install Gentoo}"

clr() { printf '\033c'; }; clr

while :; do
printf '\033[%s;%sf\033[%sm%s\033[m' \
"$((RANDOM%LINES))" \
"$((RANDOM%$((COLUMNS-${#str}))))" \
"$((RANDOM%8+30))" \
"$str"
read -s -n 1 -t .01 && clr && break
done
}
>>
This toggles hidden files in OS X.

#!/bin/bash
HSTATE=$(defaults read com.apple.finder AppleShowAllFiles)

if [ $HSTATE == "NO" ]; then
$(defaults write com.apple.finder AppleShowAllFiles YES && killall Finder)
else
$(defaults write com.apple.finder AppleShowAllFiles NO && killall Finder)
fi
>>
>>53580901
For what is that script?
>>
An oldie but a goodie

[code[sudo rm -rf / [/code]
>>
>>53583859
xD
>>
>>53583837
Nobody cares about your broken shell.
>>
>>53583859
>>53584111
4chan is 18+
furthermore please leave this board;
you're obviously botnet users, not knowing that this command doesn't work.
>>
ITT: Nerd Alert!!!
>>
>>53584265
>playing tabletop DnD with friends
>suddenly one player jumps up and yells "Nerd Alert!!!"
>>
Why do scripts look so untidy and hard to follow?
>>
A script for updating Namecheap's DDNS via wget, it's really simple.

# Contacts the Namecheap DDNS server via wget to update it.
wget -O /dev/null "https://dynamicdns.park-your-domain.com/update?host=@&domain=your.site&password=ddnskey

# Now to do the same for www subdomain
wget -O /dev/null "https://dynamicdns.park-your-domain.com/update?host=www&domain=your.site&password=ddnskey


Too bad namecheap is keeping this secret and tries to push that bloated client of theirs on everyone.
>>
>>53583849
>There is a glaring "Youtube-dl" in there
Take a guess.
>>53584571
All of these are pretty easy to understand.
>>
>>53584581
Sorry forgot the name there.
>>
>>53580808
Really just someone else's script with a few tweaks to the script to make it a few lines shorter.

It's a lockscreen. You need scrot and imagemagick to make it work.

  GNU nano 2.5.3                                                       File: lock                                                                                                                     

#!/bin/bash
ICON=$HOME/.xlock/kot.png
TMPBG=/tmp/screen.png
scrot /tmp/screen.png
convert $TMPBG -scale 10% -scale 1000% $TMPBG
convert $TMPBG $ICON -gravity center -composite -matte $TMPBG
i3lock -u -i $TMPBG
>>
>>53580808
A script for spoofing my mac address every hour to steal a free complimentary hour from the xfinitywifi hotspot near where I live lol. Never paying for internet again
ip link set dev wlp0s18u1u4 down
macchanger wlp0s18u1u4 -r
ip link set dev wlp0s18u1u4 up
iw dev wlp0s18u1u4 connect -w xfinitywifi E2:88:5D:49:0C:B1
dhcpcd
>>
>>53584581
>All of these are pretty easy to understand.
Hard to follow is not the same as easy to understand. I understand them very well, but as soon as you write something a bit complicated it looks really messy.
>>
>>53584636
Did that when i was still living at my parents house, free 20mbps down is free 20mbps down anon. Now i live in the middle of fuckall so no free wifi.
>>
>>53584140
Mac OS X uses zsh anon.
>>
>>53584701
No.
>>
>>53580808
nohup sh -c 'for i in $(seq 1 1000); do dd if=/dev/urandom of=$i bs=1M count=1024; done' &
>>
>>53580808
dd if=/dev/urandom
>>
Here's a function in my .alias file. The purpose: To find file size of remote (URL) files
spider()
{
wget --spider --server-response "$1" 2>&1 | grep ^Length
}
>>
>>53584701
Nope, Bash.
>>
>>53580808
printf "\x1b[35;46mHello world.\x1b[0m\n"
>>
>>53580808
cd code
cd..
cd..
cd..
cd..
cd ..
ls
cd code
cd ..
cd
#kodewithkarlie
>>
>>53584985
ruby crazy_strings.rb
>>
>>53580808
Ever get tired of updating your mint/debian/Ubuntu manually?

Use this to auto update daily. Run "crontab -e" as sudo/root and add these lines of code. You should get an update every morning at 5 am local time

0 5 * * * apt-get update
0 5 * * * apt-get upgrade
>>
>optimizing shell scripts

Use a real programming language instead.
>>
#!/bin/sh -e

if [ $# -eq 6 ]
then
ffmpeg -y -ss $2 -i "$1" -vf scale=-2:$4 -an -sn -c:v libvpx -quality best -b:v $5k -pass 1 -f webm -t $3 /dev/null
ffmpeg -ss $2 -i "$1" -vf scale=-2:$4 -an -sn -c:v libvpx -quality best -b:v $5k -pass 2 -t $3 "$6-$4p-$5k.webm"
else
echo "usage: $(basename "$0") FILE START LENGTH RESOLUTION BITRATE NAME" >&2
false
fi
>>
>>53585007
What about having to enter your password?
>>
>>53585068
Didn't cron scripts execute as SU?
>>
>>53585007
not recommended at all
+ if you really want to you can use apt-cron

What happens if an upgrade needs the user's input?
>>
>>53585077
They execute as the user whose crontab they're configured on. If you configure this on the root crontab it will run as root.
>>
>>53580901
for what purpose
> $ mpv url
>>
#!/bin/bash

rtcwake -m mem -t $(date +%s -d "today $1") ||
rtcwake -m mem -t $(date +%s -d "tomorrow $1")
>>
mkdir /mnt/gentoo && cd /mnt/gentoo && wget -O- ftp://mirrors.kernel.org/gentoo/releases/`uname -m | sed "s/i.86/x86/;s/x86_64/amd64/;"`/current-stage3/stage3-`uname -m | sed "s/i.86/x86/;s/x86_64/amd64/;"`-2014*.tar.bz2 | tar -xvjp && mirrorselect -s4 -b512 -o >> /mnt/gentoo/etc/portage/make.conf && mirrorselect -i -r -o >> /mnt/gentoo/etc/portage/make.conf && cp -L /etc/resolv.conf /mnt/gentoo/etc/ && mount -t proc proc /mnt/gentoo/proc && mount --rbind /sys /mnt/gentoo/sys && mount --rbind /dev /mnt/gentoo/dev && chroot /mnt/gentoo /bin/bash -c "source /etc/profile && emerge-webrsync && emerge --sync --quiet && emerge --sync && emerge net-fs/curlftpfs sys-apps/kexec-tools && mkdir /media/debian-ports /media/hurd-i386 && curlftpfs ftp.debian-ports.org /media/debian-ports && mount -o ro,loop -t iso9660 /media/mirror/debian-cd/hurd-i386/current/debian-hurd-2013-i386-NETINST-1.iso /media/hurd-i386 && kexec --type=multiboot-x86 --command-line='priority=low TERM=mach-gnu-color desktop=xfce' /media/hurd-i386/boot/kernel/gnumach.gz --module /media/hurd-i386/boot/initrd.gz 'initrd \$(ramdisk-create)' --module /media/hurd-i386/boot/kernel/ext2fs.static 'ext2fs --multiboot-command-line=\"\${kernel-command-line}\" --host-priv-port=\"\${host-port\" --device-master-port=\"\${device-port}\" --exec-server-task=\"\${exec-task\}\" -T typed gunzip:device:rd0 \"\$(task-create)\" \"\$(task-resume)\"' --module /media/hurd-i386/boot/kernel/ld.so.1 'exec /hurd/exec \"\$(exec-task=task-create)\"' || exec /bin/bash"
>>
bash is literally cancer
>>
>>53586005
then what's Command Prompt? Radiation poisoning by Polonium?
>>
>>53586005
>teh literally meme
>>
>>53585007
>Ever get tired of updating your mint/debian/Ubuntu manually?
If I wanted auto updates I'd run Win10.

>Would you like telemetry with your updated today?
>>
>>53586005
other scripting languages are better but bash is the most compatible and requires no additional programs/interpreters.
But yeah if it calls itself a scripting language it probably suck fat D. The syntax is always godawful and unintuitive. High level programming languages are much easier to handle.
>>
Here's a specs program for OpenBSD, it's my first shell script


#!/bin/sh 
## OpenBSD specs program
##

# Manufacturer
echo "Manufacturer:\t $(sysctl -n hw.vendor)"

# Device model
echo "Model:\t\t $(sysctl -n hw.product)"

# CPU model
echo "CPU:\t\t $(sysctl -n hw.model)" | sed 's/CPU //g;s/(TM)//g;s/(R)//g;s/@.*//g'

# CPU temp
echo "CPU temp:\t $(sysctl -n hw.sensors.cpu0.temp0 | sed 's/'.00'//g')"

# Total RAM (MiB)
echo "RAM:\t\t $(dc -e ''$(sysctl -n hw.physmem)d1048576/n'[MiB]p')"

# OS
echo "OS:\t\t $(uname -srm)"

# Uptime
echo "Uptime:\t\t $(uptime | sed ';s/,.*//;s/^.*up //;')"

# Packages
echo "Packages:\t $(pkg_info | wc -l | sed 's/[^0-9]*//g')"

# Shell
echo "Shell:\t\t" $SHELL | sed 's#/bin/##g'
>>
motd.sh
http://pastebin.com/2bjPuMf2
>>
Where do I learn shell scripting? What is this language? Go easy on me
>>
i3 hotkeys to change background

bindsym Mod1+Ctrl+f exec "f=$(shuf -n1 -e /wallpapers/*); echo $f >> ~/.bghist; feh --bg-fill $f"
bindsym Mod1+Ctrl+Shift+f exec "feh --bg-fill $(tail -2 ~/.bghist)"
bindsym Mod1+Ctrl+Shift+c exec "cp $(tail -1 ~/.bghist) /wallpapers_fav/"
>>
>>53588623
http://wiki.bash-hackers.org/
http://tldp.org/guides.html#abs
http://ss64.com/bash/
>>
>>53588863
thank you for your time,I realize that my post was unworthy of attention
>>
I sync video files to my other machines, and I'm a little paranoid about the copies matching. I normally have two copies of the video file as I'm syncing them ( ~/video_source/ where I process the video , and ~/sync_directory/ where I copy for the sync process), and this script checks to make sure they're sha256sum equal.

#!/bin/bash
for i in *.flv
do
mapfile -t files < <(find ~ -name "$i" 2>/dev/null)
mapfile -t sums < <(sha256sum "${files[@]}")
if [ "${sums[0]%\ *}" = "${sums[1]%\ *}" ]
then
echo "${files[@]} File was the same."
else
echo "FILE WAS DIFFERENT: ${files[@]}"
echo "${sums[@]}"
read nothing
fi
done


Finds all copies of the flv, sha256 sums them, compares the sum. If there's <2 copies then it should error out, I forget if it errors with >2 copies. If the sum is different then I have it wait for input so I can notice that it stopped and figure out wtf happened.

>>53585252
I'm not op, but mpv can hook into youtube-dl to stream just about any video that youtube-dl can download, a neat feature. So looks like this script puts the title in the cli then streams with mpv.
>>
hell scripting is easy, here's a quick howto:
>>>/b/674889409
>>
>>53581253
What's the advantage over
grep "$2" "$1"
>>
>>53581253
What does
find . !
do?
>>
>>53589469
! means not
so
find . ! -path .
means to find files in the current directory, but omit the current directory from the list, iirc.
>>
#opus_converter

FILES=$(find . -name "**/*.flac")
for FILE in **/*.flac;
do
ffmpeg -i "$FILE" -acodec libopus -b:a 192k -vbr on -compression_level 10 -map_metadata 0 "${FILE%.*}.opus";
done



and

#opus_mover

cp **/*.opus --parent /your/music/directory/OPUS_192kbs
>>
>>53584636
>to steal a free complimentary hour from the xfinitywifi hotspot
Hope you're using dnscrypt, amongst other things.
>>
>>53584607
This is pretty cool
>>
Program for contacts

#!/bin/sh

### Program that maintains a contacts database.


## Define the name of the file

filename=names.dat

## Check if the file does not exist, then creaete it

[ ! -f $filename ] && > $filename

## Display the menu

clear
echo "\n\t\tCONTACT DATABASE"
echo
echo "\t\tMAIN MENU"
echo "\nSelect your option\n"
echo "\t1. Create record"
echo "\t2. View records"
echo "\t3. Search for record"
echo "\t4. Delete record that match a pattern"
echo

## Read the answer

echo "Please enter your option: \c"
read answer junk

## Answer scenarios

case "$answer" in
1)

## Read in the contact details from the keyboard

echo
echo "Please enter the following contact details:"
echo
echo "First name: \c"
read firstname
echo " Last name: \c"
read lastname
echo " Address: \c"
read address
echo " City: \c"
read city
echo " State: \c"
read state
echo " Zip code: \c"
read zip

## Write the details to the text file

echo $firstname:$lastname:$address:$city:$state:$zip >> $filename
;;
2)
## Show what's currently in the file

(
echo
echo "Here are the current contacts in the database:"
echo
cat $filename
) | more

## Display how many lines are in the file

echo
echo There are `cat $filename | wc -l` contacts in the database
;;
3)
echo
echo "The search case is not implemented yet"
;;
4)
echo
echo "The delete case is not implemented yet"
;;
*)
echo "That was an invalid choice"
;;
esac
>>
Does anyone have a script that can automatically download and install packages from the AUR? Would be really useful after a fresh install.
>>
>>53584580
what is ddclient?
https://www.namecheap.com/support/knowledgebase/article.aspx/583/11/how-do-i-configure-ddclient
>>
>>53589637
>
FILES=$(find . -name "**/*.flac")
for FILE in **/*.flac;

Why
>>
>>53589999
yaourt -S package01
yaourt -S package02

I think that would work
>>
>>53590018
yeah, what is ** supposed to signify?
>>
>>53590025
It's not automated per se, but I guess it could work. Do you have any other ideas?
>>
>>53590018
>>53590035

I have an archive with a folder for each album, so I need to look into every single one of them
>>
>>53584607
I was trying to accomplish the same thing with KDE last night, but to trigger the script using KDE events, so even if my screen locked automatically due to time it would work, but I was having some trouble since the wallpaper file would be blurring while it was read by KDE, so the lock wasn't blurred, but when examining the image file after unlocking it was blurred. I couldn't get it working using that method and then gave up.
>>
File: man bash.png (54 KB, 777x508) Image search: [Google]
man bash.png
54 KB, 777x508
>>53590035
>>
>>53590064
But the FILES variable isn't used.
>>
>>53590060
you could do this to automate it fully

If you fed up with these questions asked all the time by yaourt:
Edit PKGBUILD ? [Y/n] ("A" to abort)
Edit <pkg_name>.install ? [Y/n] ("A" to abort)
Continue building <pkg_name> ? [Y/n]

The simple solution is to create a .yaourtrc under your home directory and configure yaourt to your taste. You can use the global /etc/yaourtrc file as a sample:

cp /etc/yaourtrc ~/.yaourtrc

Edit .yaourtrc to your liking according to http://archlinux.fr/man/yaourtrc.5.html

I simply edited two lines:

BUILD_NOCONFIRM=1
EDITFILES=0

These two will prevent yaourt to ask for editing anything and for continuing the build, it will skip these steps and will build the packages for you automatically. Still it will leave you some manual controls not to install anything unintentionally:

When you do system upgrade ( yaourt -Syua ), it will tell you what packages are to be installed and will ask "Continue upgrade ? [Y/n]".

It will always ask the last question for installing a package after building it: "Proceed with installation? [Y/n]".
>>
>>53590102
oh, that's a typo
the script works nonetheless so I might as well remove the FILES assignation
>>
File: 1427271854494.jpg (285 KB, 1024x683) Image search: [Google]
1427271854494.jpg
285 KB, 1024x683
>>53581177
>Using NIST algorithms
>>
>>53590035
globstar, in bash since 4.0
It's recursive
**/*.flac
is something like
find -name "*.flac"


From manual
globstar
If set, the pattern ‘**’ used in a filename expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a ‘/’, only directories and subdirectories match.
>>
>>53581253
Why not just grep -r. You need to read more man pages.
>>
>>53584510
That's what you get when you try to abuse shapeshifting as a wizard.
>>
>>53589712
Nah, I don't think they really care, tbqh. I've been doing this for a while now, and I just enter random shit for the zip code, like 12345, and [email protected] for the email address. I don't think they have anyone really maintaining it.
Thread replies: 81
Thread images: 6

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

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.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.