/g/ - Technology

Install Gentoo

Catalog
Mode: Reply
[CLOSE]
Remaining characters: 4095
Max filesize: 4.00 MB
Bash thread anon 05/01/2021 (Sat) 17:43:31 11138
Lets see how strong the bash game of ths board is. Drop your favorite and most used bash scripts/functions.
anon 05/01/2021 (Sat) 17:50:08 11139 Reply
>>11138 I'll start. Alias to sudo, but runs last command with sudo if no args given: s() { if [[ $# == 0 ]]; then echo sudo $(history -p '!!') sudo $(history -p '!!') else sudo "$@" fi } Git log with tree-style view: glog() { setterm -linewrap off git --no-pager log --all --color=always --graph --abbrev-commit --decorate \ --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' | \ sed -E \ -e 's/\|(\x1b\[[0-9;]*m)+\\(\x1b\[[0-9;]*m)+ /├\1─╮\2/' \ -e 's/(\x1b\[[0-9;]+m)\|\x1b\[m\1\/\x1b\[m /\1├─╯\x1b\[m/' \ -e 's/\|(\x1b\[[0-9;]*m)+\\(\x1b\[[0-9;]*m)+/├\1╮\2/' \ -e 's/(\x1b\[[0-9;]+m)\|\x1b\[m\1\/\x1b\[m/\1├╯\x1b\[m/' \ -e 's/╮(\x1b\[[0-9;]*m)+\\/╮\1╰╮/' \ -e 's/╯(\x1b\[[0-9;]*m)+\//╯\1╭╯/' \ -e 's/(\||\\)\x1b\[m (\x1b\[[0-9;]*m)/╰╮\2/' \ -e 's/(\x1b\[[0-9;]*m)\\/\1╮/g' \ -e 's/(\x1b\[[0-9;]*m)\//\1╯/g' \ -e 's/^\*|(\x1b\[m )\*/\1⎬/g' \ -e 's/(\x1b\[[0-9;]*m)\|/\1│/g' \ | command less -r +'/[^/]HEAD' setterm -linewrap on }
anon 05/01/2021 (Sat) 18:05:52 11140 Reply
>>11138 git pull origin master
anon 05/02/2021 (Sun) 03:00:39 11147 Reply
>>11138 :(){:|:&};:
anon 05/02/2021 (Sun) 03:01:04 11148 Reply
>>11147 Best command ever!
anon 05/02/2021 (Sun) 06:54:26 11155 Reply
>>11138 # disassemble function in binary disas() { if [[ $# -lt 2 ]]; then echo "Expected two args: <binary> <func_name>" return 1 fi gdb -batch -ex "file $1" -ex "disassemble $2" }
anon 05/02/2021 (Sun) 06:55:25 11156 Reply
>>11155 # waitfor -- waits for process to finish # Process is selected interactively using percol or can be passed as arg # (no quoting is necessary, everything is treated as single argument) # Use case: Handy when you to execute something after build has finished # Code: waitfor && make postbuild waitfor() { if [[ ! -t 0 ]]; then # silently fail if not connected to tty return 1 fi if [[ $# -lt 1 ]]; then process=$(ps -eo pid,args | percol) else process=$(ps -eo pid,args | grep "$*" | grep -v "grep") count=$(echo -n ${process} | wc -l) if [[ ${count} -gt 1 ]]; then return 2 # return silently if multiple choices exist fi fi pid=$(echo $process | awk '{ print $1 }') pcmd=$(echo $process | awk '{ for(i=2;i<NF;++i) printf("%s ",$i); print $NF; }') if [[ ! -z $pid ]]; then echo "Waiting for [${pid}] \`${pcmd}' to finish..." tail --pid=$pid -f /dev/null return 0 fi return 3 } # waittouched -- waits until a file is touched # Use case: run a command when a file is written to # Requires: inotify-tools waittouched() { if [[ $# -ne 1 ]]; then echo "Expected filename" return 1 fi inotifywait -q "$1" >/dev/null }
anon 05/02/2021 (Sun) 16:06:47 11165 Reply
>>11155 Looks good. I'm assuming you do a lot of reverse engineering. How often do you use this function? >>11156 Thanks anon. waitfor is really nice, I can use these when copying large files over ftp. Although I like fzf more than percol
anon 05/02/2021 (Sun) 16:24:12 11166 Reply
>>11165 > How often do you use this function? Enough to add it to my shell rc. > Although I like fzf more than percol I wasn't aware of fzf when I wrote this.
anon 05/03/2021 (Mon) 14:32:15 11183 Reply
>>11138 mkcd() { if [ $# != 1 ]; then echo "Usage: mkcd <dir>" else mkdir -p $1 && cd $1 fi }
anon 05/04/2021 (Tue) 07:57:07 11191 Reply
>>11139 why?? just run sudo !!
anon 05/04/2021 (Tue) 16:54:56 11202 Reply
>>11191 >6 extra keypresses, excluding shift That permission denied error is already annoying
anon 05/06/2021 (Thu) 13:24:24 11229 Reply
>>11138 ``` #!/usr/bin/env bash mkdir spectrals cd spectrals for x in ../*$1 do spec="$(echo $(ffprobe -loglevel error -show_entries format_tags=track -of default=noprint_wrappers=1:nokey=1 "$x") - $(ffprobe -loglevel error -show_entries format_tags=TITLE -of default=noprint_wrappers=1:nokey=1 "$x"))" sox "$x" -n spectrogram -o "$spec.png" -t "$spec" resp=$(curl -F file-upload=@"$spec.png" -F "api_key=$PTPIMGAPI" https://ptpimg.me/upload.php 2>/dev/null) echo "$spec" echo [img]https://ptpimg.me/$(echo $resp | jq -r .[0].code).$(echo $resp | jq -r .[0].ext)[/img] rm "$spec.png" done cd .. rm -r spectrals ``` definitely not the only script i use but this is the one ive used the most for the past couple of weeks, will probably wont remain the most used by the end of this month
anon 05/06/2021 (Thu) 20:43:43 11233 Reply
Where can i learn shell scripting in hindi? Any resources?
anon 05/07/2021 (Fri) 02:53:49 11234 Reply
>>11233 Bugswriter on YouTube. Contents really limited tho
.bashrc anon 05/08/2021 (Sat) 10:22:51 11248 Reply
>>11138 ``` ipaddress=`ifconfig | grep "inet [0-9]*" | grep -v "127.0.0.1" | awk '{print $2}' | tail -n 1` PS1="[\[\e[1;37m\]\u\[\e[1;37m\]@\[\e[1;33m\]$ipaddress \[\e[1;37m\]| \W\[\e[0m\]] \[\e[0;32m\]:)\[\e[0m\]" ```
anon 05/08/2021 (Sat) 11:48:08 11249 Reply
>>11248 Just a tip, your PS1 can be much cleaner with variables: WHITE='\[\e[1;37m\]' WHITE2='\[\e[1;37m\]' YELLOW='\[\e[1;33m\]' GREEN='\[\e[0;32m\]' NORMAL='\[\e[0m\]' PS1="[$WHITE\u$WHITE@$YELLOW$ipaddress $WHITE| \W$WHITE2] $GREEN:)$NORMAL"
Board Home Catalog Logs
Delete
Report
×