Shell Script\,显示选项输入的目录列表/尝试选择两个文件进行比较

Shell Script\,显示选项输入的目录列表/尝试选择两个文件进行比较

我试图让用户从编号列表中选择两个文件,该列表源自目录列表,最终将变成一个变量。我只是一名网络工程师,试图自动组合来自交换机的一些显示输出命令。

这是我所拥有的不起作用的:

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

do

当我能够从目录中选择文件时,我计划“cat $FILE1 > file1”和“cat $FILE2 > file2”,然后我将组合它们。

答案1

每一项select陈述都需要先完成,然后才能继续下一项陈述。语句select实际上是一种特殊类型的循环。

假设我有一组文件,examplefile01通过examplefile10.如果我有这样的脚本:

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

在执行中它看起来像这样:

$ ./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

break语句很重要,因为否则该select语句将循环返回以再次呈现选项。

因此,就你的情况而言,你可能需要类似以下的东西:

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

您还可以聪明一点,通过设置 PS3echo修改语句提供的提示来避开这些语句:select

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

此外,由于您计划合并文件,因此在最终选择的同时进行可能会更容易:

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

答案2

感谢您的帮助,我能够让它工作。它不漂亮,但它满足了我的要求

#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
}

相关内容