Continuing from the comment, one thing you always want to do when writing a script is to validate each step along the way. For instance, if you cannot create the new restore directory, you don't want to loop and then attempt to extract a tar.gz file to the directory that doesn't exits.
Virtually all commands return 0 on success or a nonzero error on failure. You can use this to your advantage to check if the command succeeded and if it didn't you simply exit. A quick way to do that is:
command || exit 1
In your case creating the restore directory that could be:
mkdir -p restore || exit 1
You can add additional error messages if you like, but most time the error generated by the failure will be sufficient to tell you what went wrong.
Whenever you are operating on a fixed base directory using subdirectories of that base, it is best to create a variable for that base directory you can use in your script. For example:
#!/bin/bash
userdir="${1:-/home/username/userhome}"
budir="$userdir"/backup
restore="$userdir"/restore
Here userdir is the base directory and you have additional variables for the backup directory and restore directory. This makes it convenient to reference or operate on files in any of the directories. Note also how userdir can be set from the first command line argument or uses /home/username/userhome by default if no argument is given.
You can create restore and change to that directory, validating each step as follows:
mkdir -p restore || exit 1
cd "$restore" || exit 1
For the menu, let select create the menu for you (now if you have hundreds of .tar.gz files, you may need to write a custom pager, but for a few dozen files, select will be fine). You can generate the menu and restore the selected file with:
select choice in "$budir"/*.tar.gz; do
tar -xvf "$choice" &>"$restore/restore.log"
break
done
Putting it altogether, you would have:
#!/bin/bash
userdir="${1:-/home/username/userhome}"
budir="$userdir"/backup
restore="$userdir"/restore
mkdir -p restore || exit 1
cd "$restore" || exit 1
select choice in "$budir"/*.tar.gz; do
tar -xvf "$choice" &>"$restore/restore.log"
break
done
Example Use/Output
Say I have a couple of .tar.gz files in a directory, e.g.
$ tree /home/david/tmpd/backup
backup
├── v.tar.gz
└── x.tar.gz
Then to create a restore directory under the tmpd directory I can run the script as:
$ bash ~/scr/tmp/restore.sh /home/david/tmpd
1) /home/david/tmpd/backup/v.tar.gz
2) /home/david/tmpd/backup/x.tar.gz
#? 2
By choosing 2 above the x.tar.gz file is restored under the restore directory, e.g.
$ ls -al restore/
total 4
drwxr-xr-x 3 david david 80 Oct 29 01:00 .
drwxr-xr-x 4 david david 80 Oct 29 01:00 ..
drwxr-xr-x 3 david david 60 Oct 29 01:00 home
-rw-r--r-- 1 david david 57 Oct 29 01:00 restore.log
So the restore directory was successfully created, the restore.log was created and the .tar.gz file was restored under the restore directory.
The contents of restore.log are
$ cat restore/restore.log
home/david/scr/utl/xscrd.sh
home/david/scr/utl/xzdate.sh
(which were the two sample files I added to the x.tar.gz file)
Look things over and let me know if you have further questions.
mkdir -p restorewill create therestoredirectory in whatever directory is the current directory when the script is called. Also suggestmkdir -p restore || exit 1so you exit at that point if you don't have permissions to create the directory. When youcdto the directory holding the.tar.gzfiles, suggest you useselect choice in *.tar.gz; do ... done. Theselectcommand will create a menu for you to select a file from. It will display a numbered list and you choose the number next to the filename you want,choiceholds the name.