'Shell script having problems

#!/bin/bash

while [ true ]
do
    echo "$(top -b -o +%MEM -n 1 | head -n +8 | tail -n 1 | awk '{ print $12";"$10 }');$(date +'%H:%M:%S')" >> RAM.csv
    if [ -z "$1" ]; then 
        sleep 1m
    else 
        sleep "$1"
    fi
done

ERROR OUTPUT : date: extra operand '%H:%M:%S' what is wrong with this?



Solution 1:[1]

Below you find a mildly improved version of your script:

#!/usr/bin/env bash

# All output is appended to RAM.csv
exec >> RAM.csv

# Do an infinite loop
while :; do
   # Get command with most used memory, print it in CSV format, append time
   date "+$(ps -eo ucmd -o '%mem' --sort=-%mem h | awk -v OFS=";" '($1=$1);{exit}');%T"
   # sleep $1 seconds, if not defined 1m
   sleep "${1:-1m}"
done

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kvantour