Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # tivasyk <[email protected]>
- # Dump uptime command output
- # Format: 09:31:12 up 4 days, 19:26, 2 users, load average: 0.19, 0.21, 0.15
- DUMP_ARRAY=()
- IFS=", " read -r -a DUMP_ARRAY <<< "$(uptime)"
- # Report on read tokens
- printf "Read %s tokens:\n" "${#DUMP_ARRAY[@]}"
- for ((i=0; i<"${#DUMP_ARRAY[@]}"; i++)); do
- printf " Array[%s]: %s\n" "${i}" "${DUMP_ARRAY[i]}"
- done
- # Store useful data in another array
- NAME_ARRAY=() # This will store variable names
- DATA_ARRAY=() # This will store variable data
- # Store UPTIME_DAYS
- NAME_ARRAY+=("UPTIME_DAYS")
- DATA_ARRAY+=("${DUMP_ARRAY[2]}")
- # Store UPTIME_TIME
- NAME_ARRAY+=("UPTIME_TIME")
- DATA_ARRAY+=("${DUMP_ARRAY[4]}")
- # Store LOGGED_USERS
- NAME_ARRAY+=("LOGGED_USERS")
- DATA_ARRAY+=("${DUMP_ARRAY[5]}")
- # Store CPU_LOAD
- NAME_ARRAY+=("CPU_LOAD_1M")
- DATA_ARRAY+=("${DUMP_ARRAY[9]}")
- NAME_ARRAY+=("CPU_LOAD_5M")
- DATA_ARRAY+=("${DUMP_ARRAY[10]}")
- NAME_ARRAY+=("CPU_LOAD_15M")
- DATA_ARRAY+=("${DUMP_ARRAY[11]}")
- # List saved data in a loop
- printf "Saved variables:\n"
- for ((i=0; i<"${#NAME_ARRAY[@]}"; i++)); do
- printf " Variable %s: %s=%s\n" "${i}" "${NAME_ARRAY[${i}]}" "${DATA_ARRAY[${i}]}"
- done
Advertisement
Add Comment
Please, Sign In to add comment