Good day.
I'm writing what I thought was a VERY simple script to interface the cdrecord command which is too complicated for the average user.
I want the script to run the cdrecord --scanbus and present the user with a list of burners installed on his/her system. Many PC's have multiple drives install, thus the reason I need this script. This is only the first step in this script, more interaction will follow, but I just can't get past the first step. I want to scanbus, store device names in dynamic variables and present user with the description, not the cryptic 0,0,0 or 0,1,0 or 1,0,0 device addressing.
For this example, lets say the following produce this output on my particular system
Code:
$ cdrecord --scanbus 2>/dev/null | grep "[0-9],[0-9],[0-9]"
0,0,0 0) 'LITE-ON ' 'LTR-52246S ' '6S0D' Removable CD-ROM
0,1,0 1) 'HL-DT-ST' 'DVD-ROM GDR-H30N' '1.00' Removable CD-ROM
0,2,0 2) *
<snip>
0,7,0 7) HOST ADAPTOR
So, what I want to do is capture the device "#,#,#" and the description, so the user can make an informed decision as to which device his blank CD is in.
I need to create dynamic variables, I though I knew how to do this, but for some reason they don't get exported outside of the while loop.
Here is Example that does NOT (err NOT) work:
The man script: cdmenu
Code:
#!/bin/bash
set -a
echo ""
echo "Choose a #number, 0 -> 6 to select your recording device..."
echo ""
c=0
# grep -v "*" on next line omits empty drive addr which are represented with the * symbol
cdrecord --scanbus 2>/dev/null | grep "[0-9],[0-9],[0-9]" | grep -v "*" | \
while read line; do
a=`echo ${line} | awk '{print $1}'`
b=`echo ${line} | awk '{print $2 " " $3 " " $4 " " $5}'`
eval a_${c}=\`echo \"$a\"\`
eval b_${c}=\`echo \"$b\"\`
#eval echo \"Value of a_$c: \$a_$c\"
#eval echo \"Value of b_$c: \$b_$c\"
eval echo \" \#$c -- \$b_$c\"
let c="$c + 1"
done
# Okay, at this point, I'm assuming this is what I have in my dynamic,
# variables, your milage may vari as output differes from linux/UNIX
#
# a_1 = 0,0,0
# b_1 = 'LITE-ON ' 'LTR-52246S'
#
# a_2 = 0,1,0
# b_2 = 'HL-DT-ST' 'DVD-ROM GDR-H30N'
#
# This is proven by the two lines commented out in
# the while loop
#
# Here's where I get totally stuck.
echo ""
read -p "Answer: " d
eval device=\`echo \"\$a_$d\"\`
eval description=\`echo \"\$b_$d\"\`
# debug uncomment
echo "Device value is: $device"
echo "Description value is: $description"
Results when run?
Code:
Choose a #number, 0 -> 6 to select your recording device...
#0 -- 0) 'LITE-ON ' 'LTR-52246S
#1 -- 1) 'HL-DT-ST' 'DVD-ROM GDR-H30N'
#2 -- 7) HOST ADAPTOR
Answer:
I'll Answer 0 to choose the LITE-ON
Code:
Answer: 0
Device value is:
Description value is:
My answer should be: 0,0,0, but I get nada!
What am I doing wrong?
Any takers, I'm SURE it's some darn switch I'm forgetting to add for exporting beyond a while loop? I-dunno, my head hurts.
Thanks in advance!
J.