linux - 多变量定义和赋值

标签 linux bash

我有一段脚本基本上可以计算当前目录中目录使用的空间量,但我需要帮助理解一些语法和语言礼节。

这是脚本:

#!/bin/bash

# This script prints a little histogram of how much space 
# the directories in the current working directory use

error () {
echo "Error: $1"
exit $2
} >&2

# Create a tempfile (in a BSD- and Linux-friendly way)
my_mktemp () {
mktemp  || mktemp -t hist
} 2> /dev/null

# check we are using bash 4
(( BASH_VERSINFO[0] < 4 )) && error "This script can only be run by bash 4 or higher" 1

# An array to keep all the file sizes
declare -A file_sizes
declare -r tempfile=$(my_mktemp) || error "Cannot create tempfile" 2

# How wide is the terminal?
declare -ir term_cols=$(tput cols)

# Longest file name, Largest file, total file size
declare -i max_name_len=0 max_size=0 total_size=0

# A function to draw a line
drawline () {
declare line=""
declare char="-"
for (( i=0; i<$1; ++i )); do
    line="${line}${char}"
done
printf "%s" "$line"
}

# This reads the output from du into an array
# And calculates total size and maximum size, max filename length
read_filesizes () {
while read -r size name; do
file_sizes["$name"]="$size"
(( total_size += size ))
(( max_size < size )) && (( max_size=size ))
(( max_file_len < ${#name} )) && (( max_file_len=${#name} ))
done
}

# run du to get filesizes
# Using a temporary file for output from du
{ du -d 0 */ || du --max-depth 0 *; } 2>/dev/null > "$tempfile"
read_filesizes <  "$tempfile"

# The length for each line and percentage for each file
declare -i length percentage
# How many columns may the lines take up?
declare -i cols="term_cols - max_file_len - 10"

for k in "${!file_sizes[@]}"; do
(( length=cols * file_sizes[$k] / max_size ))
(( percentage=100 * file_sizes[$k] / total_size ))
printf "%-${max_file_len}s | %3d%% | %s\n" "$k" "$percentage" $(drawline $length)
done

printf "%d Directories\n" "${#file_sizes[@]}"
printf "Total size: %d blocks\n" "$total_size"

# clean up
rm "$tempfile"
exit 0

read_filesizes()的第一行和第二行我用粗体突出显示的函数,为什么两个变量 (size name)如果 name 正在创建正在分配给 size在数组中?

在同一个函数中,(( max_size < size )) && (( max_size=size ))这条线对我来说似乎很奇怪,因为这两个表达式怎么可能都是真的?

然后在for循环的第一行,(( **length=cols** * file_sizes[$k] / max_size ))我不明白为什么变量 length分配给 cols ..为什么一开始就分别定义它们?

最佳答案

虽然我不是 100% 确定语法,但它似乎足以回答您的问题:

第一个问题

why are two variables (size name) being created if the name is being assigned to size in the array?

看起来像name保存文件名和size保存文件大小。然后赋值file_sizes["$name"]="$size"存储由文件名索引的文件大小。

第二个问题

(( max_size < size )) && (( max_size=size ))

我相信这一行指定了 sizemax_size如果以前的值为 max_size小于 size .目标是最后 max_size将容纳最大文件的大小。

第三个问题

(( length=cols * file_sizes[$k] / max_size ))

这将计算为每个文件显示的行的长度(其目标可能是说明文件与最大文件相比的相对大小)。行的长度与文件的大小有关。 cols是为最大文件(大小为 max_size 的文件)显示的行的长度。 cols = 终端的长度 - 最长文件名的长度 - 10.

关于linux - 多变量定义和赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24479490/

相关文章:

linux - Root Crontab 说在 bash 脚本中找不到命令

linux - shell脚本中双引号内的双引号

linux - 对脚本文件参数中给出的目录中的所有文件运行 cat 命令,并使用作为第二个参数给出的名称输出

linux - 使用不同的参数执行不同的命令

bash - 在 Unix/Linux 中使用命令行计算日期和时间以跟踪时间

bash - 如何使用shell脚本从句子中删除停用词?

linux - 可以在解析列之前在 awk 中搜索/替换吗?

c++ - 如何将后台运行的子进程调到前台

c - Linux 串口 : Blocking Read with Timeout

linux - 用于分析功能延迟的 SystemTap 脚本