Li *_*ang 2 size shell-script files
我目前的代码是这样的:
#!/bin/bash
while IFS= read -r line;
do
byte = $(stat -c%s "$line");
echo "$line : $byte";
done< <(ls *.$1)
Run Code Online (Sandbox Code Playgroud)
输出将是这样的:
./scan.sh cpp
./scan.sh: line 4: byte: command not found
arraysum.cpp :
./scan.sh: line 4: byte: command not found
countLines.cpp :
./scan.sh: line 4: byte: command not found
createtext.cpp :
./scan.sh: line 4: byte: command not found
multiproc1.cpp :
./scan.sh: line 4: byte: command not found
myWc.cpp :
./scan.sh: line 4: byte: command not found
test.cpp :
Run Code Online (Sandbox Code Playgroud)
基本上我的代码将采用一种语法,并将根据该语法搜索目录。问题是我希望它打印出“文件名”+“文件的字节大小”,只是我似乎无法正常工作。
在类似 Bourne 的 shell 的语法中bash,=符号输入分配周围不能有任何空格。
byte=value
Run Code Online (Sandbox Code Playgroud)
但是,在这里解析 的输出ls是一个坏主意。
你可以这样写:
#! /bin/sh -
stat -c '%n: %s' -- *."$1"
Run Code Online (Sandbox Code Playgroud)
如果您确实需要循环,只需编写它:
#! /bin/zsh -
for file in *.$1; do
stat -c '%n: %s' -- $file
done
Run Code Online (Sandbox Code Playgroud)
或者,如果您必须使用bash:
#! /bin/bash -
shopt -s failglob
for file in *."$1"; do
stat -c '%n: %s' -- "$file"
done
Run Code Online (Sandbox Code Playgroud)