获取输入参数
#!/bin/bash dir=$1 # 从命令行参数中获取目录路径 file=$2 # 从命令行参数中获取输出文件路径 echo "$dir" echo "${file}" ~
[root@node1 home]# sh t.sh ./yaml/ canshu
./yaml/
canshu
#!/bin/bash dir="/home/yaml" file="/home/yaml/ns-dev.yaml" if [ -z "$dir" ] || [ -z "$file" ]; then echo "Usage: $0 directory_path output_file_path" exit 1 fi if [ ! -d "$dir" ]; then echo "Error: $dir is not a directory" exit 1 fi if [ -e "$file" ]; then echo "Error: $file already exists" exit 1 fi cd "$dir" ls > "$file" # 将当前目录下的所有文件和目录写入到指定的文件中