简体   繁体   English

命令输出进入一个数组

[英]command output get into an array

I want to store elements of boot command.我想存储引导命令的元素。 I tried this :我试过这个:

But it doesn't work.但它不起作用。 I want to store elements of last reboot command.我想存储上次重启命令的元素。 How can I fix it ?我该如何解决?

If you want reboot times you can use awk like this:如果你想要重启时间,你可以像这样使用 awk:

# change internal field separator to newline because awk will write each date on new lines
_IFS=$IFS;
IFS=$'\n';
# Grep all lines that starts with reboot, then remove the first 2 fields and print the rest
reboots=($(last | awk '/^reboot/{$1=$2="";print}'));
# Reset IFS
IFS=_IFS;

echo ${reboots[0]}; # Mon Jul 13 10:39
echo ${reboots[1]}; # Mon Jul xx xx:xx
echo ${reboots[2]}; # Mon Jul xx xx:xx
echo ${reboots[...]}; # Mon Jul xx xx:xx

To split a string into an array using IFS as a separator, you can enclose it in parenthesis:要使用 IFS 作为分隔符将字符串拆分为数组,可以将其括在括号中:

my_array=($(last -F | grep reboot))
echo "${my_array[@]}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM