linux shell 脚本中的变量和用户输入

1 linux shell

您好,我正在尝试制作一个简短的脚本,该脚本应该以 .txt 或 .odt 等文件结尾的文件形式接受用户输入,并键入包含该目录结尾的文件数量。

所以它看起来像这样。

#!/bin/bash

echo "Enter a file ending like .txt"
read ending
for x in `ls *$ending`; do
    echo "test"
done
Run Code Online (Sandbox Code Playgroud)

gle*_*man 5

#!/bin/bash
read -p "Enter a file suffix (like .txt): " ending
files=( *"$ending" )
echo "found ${#files[@]} files with that suffix"
Run Code Online (Sandbox Code Playgroud)

不要解析ls。这将文件名存储在一个数组中,然后打印出数组的大小。