Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Stick up your best shell scripts

124»

Comments

  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    A nethack wrapper, if your nethack is built with the dumplog patch, it'll check to see whether the dumplog has been updated, and then copy it to ~/dumps/ with the date and time as filename, then make it world-readable.
    #!/bin/bash
    
    NAME="bunbun"
    DUMP="/var/games/nethack/dumps/$NAME.lastgame.txt"
    
    #Check whether the dump file exists, get last modified time if so, 0 otherwise
    if [ ! -e $DUMP ]
    then
        LDATE=0
    else
        LDATE=$(date -r $DUMP +%s)
    fi
    
    #Execute nethack with the options given
    nethack $*
    
    #If dump exists and is newer than the one we saw earlier copy the dump
    if [ -e $DUMP ]
    then
        if [ $(date -r $DUMP +%s) -gt $LDATE ]
        then
            #Only eval `date ...` once, in case the second changes between these lines
            OUTFILE="$HOME/dumps/`date +%Y-%m-%d-%H:%M:%S`.txt"
            cp $DUMP $OUTFILE
            chmod 644 $OUTFILE
        fi
    fi
    

    Just change the username and location of dumplog file at the top, and ensure the dumps directory exists.


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    Nothing super mega about this, but I use it all the time.

    Set up key exchange with a remote server:
    #!/bin/bash
    
    ssh "$1" "echo `cat ~/.ssh/id_?sa.pub` >> ~/.ssh/authorized_keys2"
    ssh "$1" "chmod 700 ~/.ssh/authorized_keys2"
    

    usage:
    ./sendKeyTo <user@remoteserver>


  • Registered Users, Registered Users 2 Posts: 218 ✭✭Tillotson


    Helps in my never-ending war against badly named mp3's. Converts files to Start Case, eg: "some file.mp3" -> "Some File.mp3".
    #!/bin/bash
    
    set -e
    
    function usage {
        echo -e "USAGE: $0 [OPTION] [FILE]"
        echo -e "Converts filename to Start Case\n"
        printf "%4s%20s\n" "-d" "dry run"
        printf "%4s%20s\n" "-s" "verbose"
        echo -e "Example: $0 *.mp3"
    
    }
    
    function err_exit {
        echo -e "$1\n"
        usage
        exit 1
    }
    
    declare dry verbose
    
    while getopts ":dv" opt; do
        case $opt in
            d) dry=y ;;
            v) verbose=y ;;
            \?) usage; exit 1 ;;
        esac
    done
    
    shift $((OPTIND-1))
    
    for f0; do
        [[ -f $f0 ]] || err_exit "$f0: is not a file"
    
        # Keep extension unchanged
        f1="$(echo $f0 | cut -d'.' -f 1)"
        ext="$(echo $f0 | cut -d'.' -f 2)"
        f1="$(echo $f1 | sed 's/\b\(.\)/\u\1/g').$ext"
    
        if [[ ! -z $dry  ]] || [[ ! -z $verbose ]]
        then
            printf "%s%40s\n" "$f0" "-> $f1"
        fi
        if [[ $f0 != $f1 ]] && [[ -z $dry ]]    # Test filenames not the same
        then
            mv "$f0" "$f1"
        fi
    done
    


  • Registered Users, Registered Users 2 Posts: 1,080 ✭✭✭KAGY


    Khannie wrote: »

    Set up key exchange with a remote server:

    <snip>
    usage:
    ./sendKeyTo <user@remoteserver>

    ssh-copy-id does the same thing, part of openssh i think,
    ssh-copy-id [-i [identity_file]] [user@]machine
    
    , though your's is easier to use as the rsa file location is hard coded


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    I use this all the time for using up all my cores on apps that aren't multithreaded (mostly we're talking about oggenc and convert (part of imagemagick)).
    #!/bin/bash
    
    NUMCPU="$(grep ^processor /proc/cpuinfo | wc -l)"
         
    until [ -z "$1" ]
    do
    	while [ `jobs -p | wc -l` -ge $NUMCPU ] ; do
    		sleep 0.1
        done
      	oggenc -q8 "$1" &
    	shift
    done
    

    Usage would be:
    oggenc_cpus.sh *.flac

    edit: be somewhat careful with hyperthreaded CPU's (mine is not).


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,929 ✭✭✭PrzemoF


    A simple script to monitor modification date of a file and trigger an action.
    Example use:
    0. Save the script to trig.sh file and make executable
    chmod a+x trig.sh
    
    1. start script with a tex file name as parameter i.e.
    ./trig.sh my_tex_file.tex
    
    2. Open the tex file in an editor:
    vim my_tex_file.tex
    
    3. Make any modifications to the file and save it - it starts pdflatex that produces my_tex_file.pdf
    4. Open evince to see the content of the pdf file
    evince my_tex_file.pdf
    

    From now on every saved modification of my_tex_file.tex will be caught by trig.sh. trig.sh will run pdflatex and evince will show updated version of the pdf file.

    trig.sh:
    #!/bin/bash
    # Delay between checks
    DELAY=1
    DATE_ORIG=$(stat -c %Y $1)
    DATE_MOD=$DATE_ORIG
    
    while [ 1 ];
    do
            if [ $DATE_ORIG -ne $DATE_MOD ]; then
                    #File changed - add your action here
                    pdflatex $1
                    DATE_ORIG=$DATE_MOD
            else
                    sleep $DELAY
                    DATE_MOD=$(stat -c %Y $1)
            fi
    done
    


  • Closed Accounts Posts: 4,763 ✭✭✭Fenster


    I blog a lot of photographs.

    1. Take in a folder name followed by a list of images as arguments.
    2. Create said, copy images to it as JPG, and rename them in sequential order.
    3. Create a smaller thumbnail size of the images to fit my blog.
    4. Move this folder to my Dropbox Public folder.
    5. Insert HTML code for the images into my clipboard.
    #!/bin/bash
    
    dropbox="$HOME/Dropbox/Public/content"
    uuid="4144919"
    thumbs="m"
    large_size=1500
    thumb_size=600
    temp="/tmp/prep"
    
    prune_spaces() {
        cp "$1" $(echo "$1" | sed -e 's/ /_/g')
    }
    
    add_line() {
        echo "<a title=\"CHANGE ME\" href=\"http://dl.dropboxusercontent.com/u/$1/content/$2/$3\"><img src=\"http://dl.dropboxusercontent.com/u/$1/content/$2/m/$3\" alt=\"CHANGE ME\" /></a>" >> $4
    }
    
    resize_image() {
        mogrify -quality 100 -format jpg -resize "$1"x\> $2
    }
    
    if [ ! -d $1 ]; then
        mkdir -p $1/$thumbs
    fi
    
    if [ -d $dropbox/$1 ]; then
        rm -r $dropbox/$1
    fi
    
    src=$1
    shift
    
    count=1
    
    for n in "$@"; do
        if [ -e "$1" ]; then
            cp "$1" $src/$count.jpg
            cp "$1" $src/$thumbs/$count.jpg
            let count++
            shift
        fi
    done
    
    cd $src
    
    if [ $(ls -1 *.jpg 2> /dev/null | wc -l) == 0 ]; then
        echo "No images selected!"
        rm -r $(pwd)
        exit 1
    fi
    
    for n in *.jpg; do
        resize_image $large_size $n
        resize_image $thumb_size $thumbs/$n
        add_line $uuid $src $n $temp
    done
    
    cat $temp | xclip -sel clip
    rm $temp
    mv $(pwd) $dropbox
    
    exit 0
    


  • Registered Users, Registered Users 2 Posts: 888 ✭✭✭Keplar240B


    My First Arch Linux script , a backup utility.
    #!/bin/bash
    
    #GWB 22-06-16
    #Backup script backs up files to remote directory
    #1: Make sure destination  is mounted
    #2: Run as root
    
    
    
    echo -e  "\e[42m******************************\e[0m"
    echo -e  "\e[42m*Written By Gwb     *\e[0m"
    echo -e  "\e[42m*Verision 3 21-06-16         *\e[0m"
    echo -e  "\e[42m*Backup script Backup_3.sh   *\e[0m"
    echo -e  "\e[42m******************************\e[0m"
    echo " "
    
    #Check that user ran as sudo
    if (( $EUID != 0 )); then
        echo -e "\e[41mPlease run as root\e[0m"
        exit
    fi
    
    #get user input for which path for backup
    Dest1="/run/media/gwb/Linux_backup"
    Dest2="/run/media/gwb/iomeaga_320"
    echo -e  "\e[44mPick destination directory for backup :--  \e[0m"
    echo "(1)   "  "$Dest1"
    echo "(2)   "  "$Dest2"
    echo "(3)    Specify a path" 
    echo "(4)    exit"
    echo ""
    echo "Press option followed by [ENTER]"
    echo ""
    read -r choice
    #check that paths exist and change path to dest
    case "$choice" in
    
    1)  echo $Dest1
        if  [ -d $Dest1 ] 
        then
          cd $Dest1
        else
    	  echo  -e "\e[91mPath not found to destination directory\e[0m"
    	  echo  -e "\e[91mNOTE : The Hard drives  internal and external must be mounted\e[0m"
              echo ""
              exit
        fi
        ;;
    
    2)  echo  $Dest2
        if  [ -d $Dest2 ] 
        then
          cd $Dest2
        else
    	  echo  -e "\e[91mPath not found to destination directory\e[0m"
    	  echo  -e "\e[91mNOTE : The Hard drives  internal and external must be mounted\e[0m"
              echo ""
              exit
        fi
        
        ;;
    3)  echo -e "\e[44mType a custom destination path\e[0m"
        read -r Path1
        if  [ -d $Path1 ] 
        then
          echo "$Path1"
          cd $Path1
        else
    	  echo  -e "\e[91mPath not found to destination directory\e[0m"
              echo ""
              exit
        fi
        ;;
    
    *) echo "exit"
        exit
       ;;
    esac
    
    #make the backup directory
    TODAYSBACKUPDATE=`date +%d-%b-%Y`
    mkdir $TODAYSBACKUPDATE
    cd $TODAYSBACKUPDATE
    echo "Backup Directory made at " "$TODAYSBACKUPDATE" at "backup path"
    echo ""
    
    #begin the backup
    echo -e "\e[30;48;5;10m1 Make copy of first 512 bytes MBR with dd\e[0m"
    dd if=/dev/sdb1 of=hda-mbr.bin bs=512 count=1
    echo -e "\e[1;10mDone.\e[0m"
    echo ""
    
    echo -e "\e[30;48;5;10m2 Make a copy of etc dir\e[0m"
    cp -a  -u /etc .
    echo -e "\e[1;10mDone.\e[0m"
    echo ""
    
    echo -e "\e[30;48;5;10m3 Make a copy of home dir\e[0m"
    cp -a  -u /home .
    echo -e "\e[1;10mDone.\e[0m"
    echo ""
    sync
    
    
    echo -e "\e[30;48;5;10m4 Make tarball of all except tmp dev proc sys run\e[0m"
    tar --one-file-system --exclude=/tmp/* --exclude=/dev/* --exclude=/proc/* --exclude=/sys/* --exclude=/run/* -pzcf RootFS_backup.tar.gz /
    echo -e "\e[1;10mDone.\e[0m"
    echo ""
    sync
    
    echo -e "\e[30;48;5;10m5 Make copy of package lists\e[0m"
    pacman -Qqen > pkglist.txt
    pacman -Qm > pkglistAUR.txt
    echo -e "\e[1;10mDone.\e[0m"
    echo ""
    
    
    
    


  • Registered Users, Registered Users 2 Posts: 888 ✭✭✭Keplar240B


    My Second Arch Linux script , a maintenace and update utility. uses cower as a AUR helper an clamav as antivirus
    #!/bin/bash
    
    #Gwb 23-06-16
    #Arch Linux distro update script
    #update system official and AUR repo using pacman and cower
    #check for broken symlinks, log errors and failed systemd services
    #and run anti virus check clam and update the database.
    echo -e "\e[40;38;5;82m*********************************\e[0m"
    echo -e "\e[40;38;5;82m*gwb 23-06-16 updatman.sh *\e[0m"
    echo -e "\e[40;38;5;82m*Arch Linux distro update script*\e[0m"
    echo -e "\e[40;38;5;82m*update system official and      *\e[0m"
    echo -e "\e[40;38;5;82m*AUR repo using cower and pacman*\e[0m"
    echo -e "\e[40;38;5;82m*check for broken symlinks,     *\e[0m"
    echo -e "\e[40;38;5;82m*log errors and failed systemd  *\e[0m"
    echo -e "\e[40;38;5;82m*services and anti virus check  *\e[0m"
    echo -e "\e[40;38;5;82m*********************************\e[0m"
    echo ""
    #cower AUR
    cd /home/gwb/Downloads
    TODAYSBACKUPDATE=`date +%d-%b-%Y`
    mkdir $TODAYSBACKUPDATE
    cd $TODAYSBACKUPDATE
    echo -e "\e[30;48;5;10m***** UPDATE COWER PACKAGES *****\e[0m"
    echo "Directory $TODAYSBACKUPDATE made at /home/gwb/Downloads for cower updates"
    echo ""
    cower -vdu
    echo ""
    # look for empty dir (if no updates) 
    if [ "$(ls -A .)" ] 
    then
         	echo -e  "\e[44mCower updates available for package build\e[0m"
         	echo -e  "\e[44mDo you wish to build and install  them now?\e[0m"
          	echo ""
    	echo "Pick Option"
    	echo "1) Yes"
    	echo "2) No"
    	echo ""
    	echo "Press 1 or 2 followed by [ENTER]"
    	echo ""
    	read choice
            if [ $choice = "1" ]
    	then
    		echo "Building and installing cower package updates"	
    		echo ""	
    		#build and install packages
                   find . -name PKGBUILD -execdir makepkg -si \;
    			
    	fi	
    
    else
        	echo ""
    	echo " No updates of AUR packages by Cower..."
    fi	
    echo ""
    echo -e "\e[1;10mDone.\e[0m"
    echo ""
    
    # -systemd --failed:
    echo -e "\e[30;48;5;10m***** ALL FAILED SYSTEMD SERVICES *****\e[0m"
    systemctl --failed --all
    echo ""
    echo -e "\e[30;48;5;10m***** FAILED ACTIVE SYSTEMD SERVICES *****\e[0m"
    systemctl --failed
    echo ""
    # -Logfiles:
    echo -e "\e[30;48;5;10m***** LOGFILES *****\e[0m"
    cd /home/gwb/Documents/Tech/Linux/MyLinux
    journalctl -p 3 -xb >errorfile
    echo -e "\e[1;10mDone.\e[0m"
    echo ""
    # Checking for broken symlinks:
    echo -e "\e[30;48;5;10m***** CHECKING FOR BROKEN SYMLINKS *****\e[0m"
    cd ~
    find . -type l -! -exec test -e {} \; -print > /home/gwb/Documents/Tech/Linux/MyLinux/log.txt
    echo -e "\e[1;10mDone.\e[0m"
    echo ""
    #pacman
    cd ~
    echo -e "\e[30;48;5;10m***** UPDATE PACMAN *****\e[0m"
    echo ""
    sudo pacman -Syu
    echo ""
    echo "Delete orphans"
    sudo pacman -Rns $(pacman -Qtdq)
    echo ""
    echo "Prune older packages from cache"
    sudo paccache -r
    echo "Writing installed package lists to file "
    sudo pacman -Qqen > /home/gwb/Documents/Tech/Linux/MyLinux/packagesQe.txt
    sudo pacman -Qm > /home/gwb/Documents/Tech/Linux/MyLinux/package.txt
    echo -e "\e[1;10mDone.\e[0m"    
    echo ""
    #Anti-virus clam Av
    # update clamscan virus definitions:
    echo -e "\e[30;48;5;10m***** UPDATING CLAMAVSCAN DATABASE *****\e[0m"
    sudo freshclam
    echo""
    echo -e "\e[30;48;5;10m***** SCANNING / RECURSIVELY WITH CLAMAV*****\e[0m"
    echo "Do you wish to run anti-virus check with clamAv at this point?"
    echo ""
    echo "Pick Option"
    echo "1) Yes"
    echo "2) No"
    echo ""
    echo "Press 1 or 2 followed by [ENTER]"
    echo ""
    read choice1
    echo "You picked option " $choice1 
    echo "" 
    if [ $choice1 = "1" ]
    	then
    		# scan entire system
    		
    		sudo clamscan --recursive=yes --infected --exclude-dir='^/sys|^/proc|^/dev|^/lib|^/bin|^/sbin' /
    		echo "" 
    		echo -e "\e[1;10mDone.\e[0m"
    			
    	else
    		echo -e "\e[1;10mDone.\e[0m"
    		exit
    fi
    
    


  • Closed Accounts Posts: 13,404 ✭✭✭✭sKeith


    Here is one I call rsync.no.quit as it will keep on going until job is finished. I needed it some time ago when I was migrating a huge amount of data off a server where rsync would keep failing with some pipe issue. (most likely flakey network or something similar)
    Set this script up with source and destination and fire and forget.

    *Warning* With the current rsync commands used Destination will mirror source. Any files or folders in destination will be destroyed /*Warning*

    #!/bin/bash
    
    source="192.168.1.1:/home/main/"
    destination="/mnt/backup/"
    
    if [ -z $source ]; then
            echo "Source unset"
            exit 1
    fi
    if [ -z $destination ]; then
            echo "Destination unset"
            exit 1
    fi
    
    echo Warning: Everything in $destination will we destroyed.
    read -n1 -t5 -r -p "CRLT+C to cancel" key
    echo
    while [ 1 ];    do
    rsync --progress --timeout=30 -a --delete-during $source $destination
    if [ "$?" = "0" ] ; then
            echo "rsync completed normally"
            exit
    else
            echo "Rsync failure. Backing off and retrying..."
            sleep 10
    fi
    done
    
    


  • Advertisement
Advertisement