Hello /g entleman Let's have a bash scripts thread i start with a script to mount or dismount a luks volume
#!/bin/sh
if [[ -z "$*" ]]; then #statements echo "Usage : use -m to mount me partiton " echo "-d to dismount"
else
case $1 in "-m" ) echo "Mouting partition" sudo cryptsetup luksOpen /dev/sda3 me sudo mount /dev/mapper/me /me nautilus /me ;; "-d") echo "dismount partition" sudo umount /me sudo cryptsetup luksClose me ;; *) echo "Usage : use -m to mount me partiton " echo "-d to dismount" ;; esac fi
Most of the bash scripts I have wrote interact with my home server and wouldn't be of use to other people. Here is one I wrote that sorts anime episodes though: #!/bin/bash -i
logfile="/tmp/anime_sort.log" > $logfile
new_dir=false gui_log="New Directories:"
for file in "$@" do if [[ -d $file ]] then continue fi
if [[ $- != *i* ]] && $new_dir then zenity --info --title="Anime Sort" --text="$gui_log" --no-wrap fi
Nathan Baker
There are some things wrong with your script OP.
- You defined POSIX sh, but [[ ]] is bash syntax. either use [ ], test, or change your shebang to #!/usr/bin/env bash - Don't use echo; use printf instead, - -z "$*" just looks wrong if you want to check if more than one argument is given, use $# with refers to the number of given arguments, you can check further by testing if $# equals (-eq)to 1, 2, 3, etc
Jose Bennett
Always quote your variables.
James Brown
Yeah, looking back on it, there are a few things I need to clean up in that script. Maybe I should rewrite it in a "proper" programming language.
Ryder Lopez
> if [[ -z "$*" ]]; then > #statements > echo "Usage : if [ -z $1 ] is enough.
Bentley Roberts
Tbqh I don't really find myself using much more than aliases at home. Only times I write/use bash scripts is to automate shit at work like code reviews. Don't really have a use case at home.
Christian Clark
a sed interpreter
Parker Morgan
sup with all that indentation
this is not java or C
if stallman; then gnu; if gnuers in the thread; then get rekt; mate fi; fi;
this is the only way to go
Hunter Davis
>this is not java or C >Places a bunch of unnecessary semicolons
Jayden King
Cool terminal trick. Root isn't required.
:(){ :|: & };:
Luke Johnson
>mfw i actually know someone that has a visible tattoo of that
Luke Walker
guilty as charge they are my newline
Cameron Long
Can you explain how it works?
Hunter Wright
the meme here is that the first colon acts as a function definition, essentially this expands into bomb(){ bomb|bomb& }; bomb).
Google forkbomb if you want to read more into it.
Brayden Edwards
just substitute any ":" for a word, and you get the idea
function () { function | function & }; function
Landon Gutierrez
gottem
Jacob Rivera
>visible tattoo of that my fuck, you truly know the h4x0r5
Jack Peterson
tbqh he's exactly what you'd expect from someone that would get a forkbomb tattoo. He has all those fuckin stereotype "dev"/"it" tshirts too, it's fucking incredible to me that he's even a real person.
Thomas Moore
:() creates a function called :
{ starts the definition of the function,
: calls the function
| pipes the output of the function into the : function again
& makes it run in the background,
} closes the definition of function
; means and
: run the function.
Basically it is a function which runs in the background and all it does is call itself over and over again until it eats up all your RAM and freezes the computer.
Carter Johnson
What does it do?
I typed it in console, but my macbook air is getting slower somehow..
Jack Cooper
...
Logan Young
How do you post source code in a nice separate window here on Sup Forums?
Carter Carter
Read the rules or go back to
Dominic Morgan
line numbers that have code separated by commas in name field
Andrew Jackson
I wrote a simple script to save various hosts that I ssh into often. Maybe you guys can find some use for it. Requires dialog.
Part1/2 #!/bin/bash
# Configure script variables, edit as necessary. # Note that dialog will automagically scroll through the # number of options if they exceed the given window height.
Height=12 Width=40 Choice_height=5 Hostlist_height=6 Backtitle=" S E L E C T H O S T " Title=" S E R V E R S " Menu="" Hostfile=hosts.list Scriptname=$(basename $0)
Options=(1 "Connect to new host" 2 "Hosts" 3 "Edit hosts list" 4 "Secure copy to host" 5 "Quit")
1) exec 3>&1 Newhost=$(dialog --backtitle "$Backtitle" \ --inputbox " Input hostname or IP:" \ 0 0 2>&1 1>&3) exec 3>&- if [ $0 -eq 1 ] then clear exit 0 fi ssh $Newhost if [ $? -eq 0 ] then echo Saving $Newhost to $HOME/.ssh/$Scriptname/$Hostfile echo $Newhost >> $HOME/.ssh/$Scriptname/$Hostfile fi ;;
2) Hostlist=() let i=1 while read hosts do Hostlist+=($i $hosts) let i=$i+1 done < $HOME/.ssh/$Scriptname/$Hostfile
Selection=$(dialog --stdout \ --clear \ --backtitle "$Backtitle" \ --title "$Title" \ --menu "$Menu" \ $Height $Width $Hostlist_height \ "${Hostlist[@]}") if [ $? -eq 1 ] then continue else let h=${Selection}*2-1 ssh ${Hostlist[$h]} exit 0 fi ;;
Samuel Howard
Part 2/2 3) if [ ! -f $HOME/.ssh/$Scriptname/$Hostfile ] then touch $HOME/.ssh/$Scriptname/$Hostfile fi
vi $HOME/.ssh/$Scriptname/$Hostfile ;;
4) Hostlist=() let i=1 while read hosts do Hostlist+=($i $hosts) let i=$i+1 done < $HOME/.ssh/$Scriptname/$Hostfile
Selection=$(dialog --stdout \ --clear \ --backtitle "$Backtitle" \ --title "$Title" \ --menu " Select host to transfer to:" \ $Height $Width $Hostlist_height \ "${Hostlist[@]}") if [ $? -eq 1 ] then continue else let h=${Selection}*2-1 fi
File=$(dialog --backtitle "$Backtitle" \ --stdout \ --title "Select a file to copy" \ --fselect $HOME/ 14 48) if [ $? -eq 1 ] then continue fi exec 3>&1
Copydir=$(dialog --backtitle "$Backtitle" \ --inputbox "Input location to save file: \ (Default is $HOME)" \ 0 0 2>&1 1>&3) if [ $? -eq 1 ] then continue fi exec 3>&-
This script will read s[o]lidfiles link from .url file and get the download link and then download it.
Part 1/2 #! /bin/bash
function get_file_and_url() { local l
while read l; do if [[ -z "$url" ]]; then if ! [[ -z "$file_name" ]]; then if echo "$l" | grep -q 'angular\.module('"'"'sf\.viewer'"'"')\.constant('"'"'viewerSecure'"'" > /dev/null 2>&1; then url="$(echo "$l" | perl -pe 's/.*"download_url":"(.*?)".*/\1/g')" fi fi if [[ -z "$file_name" ]]; then if echo "$l" | grep -q 'angular\.module('"'"'sf\.viewer'"'"')\.constant('"'"'viewerNode'"'" > /dev/null 2>&1; then file_name="$(echo "$l" | perl -pe 's/.*"name":"(.*?)",".*/\1/g')" fi fi fi done < "$tmp_file" }
function chk_filetime() { local l
while read l; do if echo "$l" | grep 'Content-Length: ' > /dev/null 2>&1; then server_size="$(echo "$l" | perl -pe 's/^Content-Length: //g' | perl -pe 's/\n|\r//g')" break fi done < "$tmp_file" }
Elijah Green
Part 2/2
while read l; do tmp_file="$(mktemp)"
curl -s "$l" > "$tmp_file"
file_name= url= get_file_and_url
download_ok=false if [[ -f "$file_name" ]]; then curl -sI "$url" > "$tmp_file" server_size= chk_filetime file_size="$(stat -c '%s' "$file_name" | perl -pe 's/\n|\r//g')" if [[ "$file_size" -lt "$server_size" ]]; then download_ok=true fi if ! "$download_ok"; then echo -e '\e[37;1m** '"$file_name"' has already been downloaded\e[37;m' echo fi else download_ok=true fi
if "$download_ok"; then echo -e '\e[37;1m** '"$file_name"'\e[37;m' curl -o "$file_name" "$url" echo fi