2

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 ?

2 Answers 2

1

If you want reboot times you can use awk like this:

# 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
Sign up to request clarification or add additional context in comments.

Comments

0

To split a string into an array using IFS as a separator, you can enclose it in parenthesis:

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

1 Comment

Okey thank you. It helps me . So if I store an array line by line, do I use awk or something different ? Do you have any idea ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.