You are not logged in.

#1 2009-10-13 02:12:09

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

[Solved] bash: How to go about creating a script to choose from a list

Tried a Google search on this and didn't get much, if I missed something really obvious please give a link, otherwise I'd really appreciate any help.  I'd like to be able to build a script that will check files names for their appended date and allow the user to be able to choose which one of them to restore from a list.  Perhaps, it's better if I just put up the script:

...
bacdir=/root/backup/kde4-plasma-rcs
plasmarcs=$(find $HOME/.kde4/share/config -type f -name "plasma*")
date=$(date +%F)

# Check for differences in files, only backup if there are


case $1 in
  r | restore ) echo "Choose the plasma configuration you'd like to restore"
                read selection
                if [[ $selection ==
                ;;
  * )           for rc in "$plasmarcs"; do
                  cp "$rc" "$bacdir"/"$rc"-"$date"
                done
                ;;
esac

And that's as far as I've gotten.  I thought about doing something like `ls ${backdir##*-}` then finding a way to put that in a list not sure how to do that.  Are there better ways to do this, then allow an action to be takenafterword.

Last edited by Gen2ly (2009-10-13 17:31:27)


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#2 2009-10-13 02:53:28

rowdog
Member
From: East Texas
Registered: 2009-08-19
Posts: 118

Re: [Solved] bash: How to go about creating a script to choose from a list

Since you already found the list with find, maybe something like this would do.

for rc in $plasmarcs; do echo $rc; done

edit: fixed wrong var name

Last edited by rowdog (2009-10-13 02:54:36)

Offline

#3 2009-10-13 02:54:08

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [Solved] bash: How to go about creating a script to choose from a list

Apart from zenity and dmenu, I'd use something like this:

IFS='
'
plasmarcs=($(find "$backdir" -type f -name "plasma*")) # put results in an array
unset IFS
for((i=0;i<${#plasmarcs[@]};i++)) ; do
    echo "${i}) ${plasmarcs[$i]}"
done
echo -n "Choose the plasma... "
read n
selection="${plasmarcs[$n]}"
echo "$selection" # do something else

This is what I used in my apple trailer script too.
you can also use `ls -1 "$backdir"/*` instead of find if backups are all in one dir.


This silver ladybug at line 28...

Offline

#4 2009-10-13 13:57:56

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved] bash: How to go about creating a script to choose from a list

Not sure why I didn't think of the trailer script before... roll.  Good news though is that I did get it done.  Not quite sure how the i=0;i<:i++ works just yet but got the thought.  Here's the script for anyone interested:

#!/bin/bash
# plasmabak - backup and restore local plasma config

# text color variables
bldylw='\e[1;33m' # yellow
bldblu='\e[1;34m' # blue
bldwht='\e[1;37m' # white
txtrst='\e[0m'    # text reset

backdir=/root/backup/kde4-plasma-rcs
userdir=/home/todd
plasmarcs=($(find "$userdir"/.kde4/share/config -type f -name "plasma*"))
bkpplasmarcs=($(find "$backdir" -type f -name "plasma*"))
date=$(date +%F)

# Display usage if full argument isn't given
if [[ -z "$@" ]]; then
  echo " ${0##*/} b|r - backup or restore plasma configurations"
  exit
fi

# Create backup directory if it doesn't exist
if [[ ! -d "$backdir" ]]; then
  mkdir "$backdir"
  echo "${bldblu}*${txtrst} KDE 4 plasma config backup directory doesn't exist.  Created."
fi

# Delete backups older than two months
if [[ -n "$(find "$backdir" -mtime +60)" ]]; then
  find "$backdir" -mtime +60 -exec rm {} \;
  echo "${bldblu}*${txtrst} Configurations older than two months deleted"
fi

case $1 in
  b | backup )  tar -czvpf "$backdir"/plasmarcs-"$date".tar.gz ${plasmarcs[@]}
                ;;
  r | restore ) for((i=0;i<${#bkpplasmarcs[@]};i++)) ; do
                  echo -e " ${bldwht}${i}) ${bkpplasmarcs[$i]##*/}${txtrst}"
                done
                echo -n "Choose the plasma configuration to restore: "
                read restore
                selection="${bkpplasmarcs[$restore]}"
                tar -xvf "$selection" -C /
                echo -e " ${bldwht}*${txtrst} Restored plasma configs: ${bldwht}${bkpplasmarcs[$restore]##*/}${txtrst}"
                ;;
  * )           echo -e " ${bldwht}*${txtrst} Use 'plasmabak b' to backup and 'plasmabak r' to restore"
esac

Appreciate the help, lolilolicon.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

Board footer

Powered by FluxBB