0

I am trying to have the user select two files from a numbered list which is derived from a directory listing and will eventually be turned into a variable. Im just a network engineer trying to automate combining some show output commands from a switch.

This is what I have that is not working:

echo "Please Select the Show interface status file"
select FILE1 in *;
echo "Please Select the Show Vlan file"
select FILE2 in *;

do

When I am able to select files from the directory, I plan to "cat $FILE1 > file1" & "cat $FILE2 > file2" then I will combine them.

  • Hi @dis0wned. Your questions is completely unclear. kindly, would you please include example for the input and the expected output. this may help to clarify things! We appreciate your clarifications! – user88036 Sep 21 '18 at 18:00
  • 1
    "Completely unclear" may be an overstatement; I believe I got the gist of the question quite readily. – DopeGhoti Sep 21 '18 at 18:12
2

Each select statement needs to be completed before moving on to the next one. A select statement is actually a special type of loop.

Let's say that I have a set of files, examplefile01 through examplefile10. If I had a script like this:

select f in example*; do
  echo "You selected $f"
  break
done

It would look like this in execution:

$ ./470595.sh
1) examplefile01    4) examplefile04   7) examplefile07  10) examplefile10
2) examplefile02    5) examplefile05   8) examplefile08
3) examplefile03    6) examplefile06   9) examplefile09
#? 5
You selected examplefile05

The break statement is important, because otherwise the select statement would loop back to presenting the options again.

So in your case, you might want something like:

echo "Please Select the Show interface status file"
select FILE1 in *; do
    cat "$FILE1" >> outputfile1
    break
done

echo "Please Select the Show Vlan file"
select FILE2 in *; do
    cat "$FILE2" >> outputfile2
    break
done

You can also get a little clever and eschew the echo statements by modifying the prompt provided by the select statement by setting PS3:

PS3="Please Select the Show interface status file )"
select FILE1 in *; do
    cat "$FILE1" >> outputfile1
    break
done

PS3="Please Select the Show Vlan file )"
select FILE2 in *; do
    cat "$FILE2" >> outputfile2
    break
done

Also, since you're planning on combining the files, it might be easier to do that at the same time as the final selection:

PS3="Please Select the Show interface status file )"
select FILE1 in *; do
    break
done

PS3="Please Select the Show Vlan file )"
select FILE2 in *; do
    cat "$FILE1" "$FILE2" > outputfile
    break
done
0

Thank you for the help, I was able to get it working. Its not pretty but it does what I want

#Combine Show Vlan and Show interface status Function
combinevlanshint()
{
cd $shintstatvlan
clear
#Ask for Hostname 
echo "Names can not contain spaces:"
echo " "
echo "Please enter the Hostname"
read "hostname" 
clear
echo "Please Select the Show interface status file"
select FILE1 in *; do
    cat "$FILE1" > $shintstatvlan/file1
    break
done
echo "Please Select the Show Vlan file"
select FILE2 in *; do
    cat "$FILE2" > $shintstatvlan/file2
    break
    done

echo "You picked $FILE1 and $FILE2 , These files will now be combined. Press any key to continue"
read -n 1



     cat $FILE1 > file1
     cat $FILE2 > file2
sed 's/[[:space:]]*,[[:space:]]*/,/g' file1 > file1.$$ && awk -F, 'FNR==NR{f2[$1]=$2;next} FNR==1{print $0, "VLAN Name";next} {print $0,($5 in f2)?f2[$5]:"NA"}' OFS=, file2 file1.$$ > file3 && rm file1.$$
mv file3 
mv --backup=t $shintstatvlan/file3 $outputdir/$hostname.shintstatwvlans.txt
rm $shintstatvlan/file1 $shintstatvlan/file2
break
clear
mainmenu
}
  • 1
    Indentation is worth working on, as it makes code so much clearer to read (and maintain). Ensure your first line is #!/bin/bash to tell the kernel that it's a bash shell script. Continue the (good) habit of double-quoting strings that use variables - you've got most of them here but not all. In particular remember quotes for mv --backup=t "$shintstatvlan/file3" "$outputdir/$hostname.shintstatwvlans.txt" and rm "$shintstatvlan/file1" "$shintstatvlan/file2". – roaima Sep 21 '18 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.