使用bash打印表格数据以适应屏幕宽度

使用bash打印表格数据以适应屏幕宽度,bash,printf,tabular,Bash,Printf,Tabular,我正在打印用bash脚本处理的文件的名称。因为有许多文件路径正在处理中。我决定让它们像ls命令一样打印,该命令根据屏幕宽度打印表格。但我不知道我的屏幕上可以有多少列,并且在显示器上可以完全不同。有没有自动的方法呢?或者是一个简单的函数,可以计算宽度并和字符串的大小进行比较 printf "%s\t" "$FILENAME" 通常使用tput程序完成此工作,如下所示: #!/bin/sh columns=$(tput cols) lines=$(tput lines) printf "ter

我正在打印用bash脚本处理的文件的名称。因为有许多文件路径正在处理中。我决定让它们像
ls
命令一样打印,该命令根据屏幕宽度打印表格。但我不知道我的屏幕上可以有多少列,并且在显示器上可以完全不同。有没有自动的方法呢?或者是一个简单的函数,可以计算宽度并和字符串的大小进行比较

printf "%s\t" "$FILENAME"

通常使用
tput
程序完成此工作,如下所示:

#!/bin/sh

columns=$(tput cols)
lines=$(tput lines)

printf "terminal dimensions are: %sx%s\n" "$columns" "$lines"
#!/bin/sh

MYSTRING="This a long string containing 38 bytes"

printf "Length of string is %s\n" "${#MYSTRING}"
要确定字符串中的字符数,请执行以下操作:

#!/bin/sh

columns=$(tput cols)
lines=$(tput lines)

printf "terminal dimensions are: %sx%s\n" "$columns" "$lines"
#!/bin/sh

MYSTRING="This a long string containing 38 bytes"

printf "Length of string is %s\n" "${#MYSTRING}"
利用shell使用
${var}
测量字符串的能力。 下面是一个将这些技术结合在一起以格式化和居中文本的示例:

#!/bin/sh

columns=$(tput cols)
lines=$(tput lines)
string="This text should be centered"
width="${#string}"
adjust=$(( (columns - width ) / 2))
longspace="                                             "

printf "%.*s" "$adjust" "$longspace"
printf "%s" "${string}"
printf "%.*s" "$adjust" "$longspace"
printf "\n"

通常使用
tput
程序完成此工作,如下所示:

#!/bin/sh

columns=$(tput cols)
lines=$(tput lines)

printf "terminal dimensions are: %sx%s\n" "$columns" "$lines"
#!/bin/sh

MYSTRING="This a long string containing 38 bytes"

printf "Length of string is %s\n" "${#MYSTRING}"
要确定字符串中的字符数,请执行以下操作:

#!/bin/sh

columns=$(tput cols)
lines=$(tput lines)

printf "terminal dimensions are: %sx%s\n" "$columns" "$lines"
#!/bin/sh

MYSTRING="This a long string containing 38 bytes"

printf "Length of string is %s\n" "${#MYSTRING}"
利用shell使用
${var}
测量字符串的能力。 下面是一个将这些技术结合在一起以格式化和居中文本的示例:

#!/bin/sh

columns=$(tput cols)
lines=$(tput lines)
string="This text should be centered"
width="${#string}"
adjust=$(( (columns - width ) / 2))
longspace="                                             "

printf "%.*s" "$adjust" "$longspace"
printf "%s" "${string}"
printf "%.*s" "$adjust" "$longspace"
printf "\n"

通常使用
tput
程序完成此工作,如下所示:

#!/bin/sh

columns=$(tput cols)
lines=$(tput lines)

printf "terminal dimensions are: %sx%s\n" "$columns" "$lines"
#!/bin/sh

MYSTRING="This a long string containing 38 bytes"

printf "Length of string is %s\n" "${#MYSTRING}"
要确定字符串中的字符数,请执行以下操作:

#!/bin/sh

columns=$(tput cols)
lines=$(tput lines)

printf "terminal dimensions are: %sx%s\n" "$columns" "$lines"
#!/bin/sh

MYSTRING="This a long string containing 38 bytes"

printf "Length of string is %s\n" "${#MYSTRING}"
利用shell使用
${var}
测量字符串的能力。 下面是一个将这些技术结合在一起以格式化和居中文本的示例:

#!/bin/sh

columns=$(tput cols)
lines=$(tput lines)
string="This text should be centered"
width="${#string}"
adjust=$(( (columns - width ) / 2))
longspace="                                             "

printf "%.*s" "$adjust" "$longspace"
printf "%s" "${string}"
printf "%.*s" "$adjust" "$longspace"
printf "\n"

通常使用
tput
程序完成此工作,如下所示:

#!/bin/sh

columns=$(tput cols)
lines=$(tput lines)

printf "terminal dimensions are: %sx%s\n" "$columns" "$lines"
#!/bin/sh

MYSTRING="This a long string containing 38 bytes"

printf "Length of string is %s\n" "${#MYSTRING}"
要确定字符串中的字符数,请执行以下操作:

#!/bin/sh

columns=$(tput cols)
lines=$(tput lines)

printf "terminal dimensions are: %sx%s\n" "$columns" "$lines"
#!/bin/sh

MYSTRING="This a long string containing 38 bytes"

printf "Length of string is %s\n" "${#MYSTRING}"
利用shell使用
${var}
测量字符串的能力。 下面是一个将这些技术结合在一起以格式化和居中文本的示例:

#!/bin/sh

columns=$(tput cols)
lines=$(tput lines)
string="This text should be centered"
width="${#string}"
adjust=$(( (columns - width ) / 2))
longspace="                                             "

printf "%.*s" "$adjust" "$longspace"
printf "%s" "${string}"
printf "%.*s" "$adjust" "$longspace"
printf "\n"

在Bash中,您可以从
内置变量中获取屏幕宽度和高度。
如另一个答案所述,您可以使用
${var}


您可能会发现古老而久负盛名的
pr
实用程序对于在列中生成输出非常有用。

在Bash中,您可以从
列和
行中获取屏幕宽度和高度
内置变量。
如另一个答案所述,您可以使用
${var}


您可能会发现古老而久负盛名的
pr
实用程序对于在列中生成输出非常有用。

在Bash中,您可以从
列和
行中获取屏幕宽度和高度
内置变量。
如另一个答案所述,您可以使用
${var}


您可能会发现古老而久负盛名的
pr
实用程序对于在列中生成输出非常有用。

在Bash中,您可以从
列和
行中获取屏幕宽度和高度
内置变量。
如另一个答案所述,您可以使用
${var}

您可能会发现古老而古老的
pr
实用程序对于生成列中的输出非常有用