To parse the command output and store each entry in a separate variable in a shell script for systemd-boot, you can use a combination of while read and IFS (Internal Field Separator) to split the output into individual lines. Here's an example of how you can modify your script:
#!/bin/bash
# Reset IFS to newline character
IFS=$'
'
# Get the entries from systemd-boot list-units command
entries=$(systemd-boot list-units | grep -v "active" | grep -v "inactive")
# Loop through each entry and store in a variable
for entry in $entries; do
entry_name=$(echo $entry | awk '{print $1}')
entry_state=$(echo $entry | awk '{print $3}')
# Process each entry as needed
echo "Entry Name: $entry_name"
echo "Entry State: $entry_state"
# You can add more commands here to process each entry
done
In this script, we first reset the IFS variable to a newline character. Then, we use the systemd-boot list-units command to get the list of entries, excluding the active and inactive ones. We pipe the output to grep -v twice to filter out the active and inactive entries.
Next, we loop through each entry using a for loop. Inside the loop, we use awk to extract the name and state of each entry. We store these values in separate variables, entry_name and entry_state, respectively.
Finally, we print the name and state of each entry for demonstration purposes. You can replace the echo commands with your own processing logic as needed.
This script should help you parse the command output and store each entry in a separate variable. Make sure to save this script as a .sh file, give it execute permissions, and run it with ./yourscript.sh.
References: