# !/bin/sh
echo "Enter file name:"
read fname
set ${ls -la $fname}
echo "The size of test.sh is $5 byte"
exit 0我想写一个可以在linux shell脚本中使用'set‘命令打印文件大小的代码,所以我使用了ls -la,但它不起作用,我的终端只在第4行显示了’糟糕的替换‘。如有任何帮助,请:)
发布于 2021-05-04 16:16:08
我建议尝试以下几种方法:
# !/bin/sh
echo "Enter file name:"
read fname
SIZE=$(ls -l "${fname}" |awk '{print $5}')
echo "The size of ${fname} is ${SIZE} bytes"
exit 0SIZE变量将包含读取的文件名的大小。请注意,与ls -l一样,ls -al也显示隐藏文件(以“.”开头的文件)。
使用set定义变量并不是真正的最佳实践。Here's a suggestion about the usage of set。
发布于 2021-05-04 16:44:30
关注并纠正卡斯特尔我更愿意说:
# !/bin/sh
read -p "Enter file name: " fname
SIZE=$(ls -l "$fname" | awk '{print $5}')
echo "The size of $fname is $SIZE bytes"https://stackoverflow.com/questions/67380880
复制相似问题