Linux fügt einer Befehlsausgabe eine Zeichenfolge hinzu, bevor die Ausgabe in einer Datei gespeichert wird

Linux fügt einer Befehlsausgabe eine Zeichenfolge hinzu, bevor die Ausgabe in einer Datei gespeichert wird
$ ps -p 31690 -o %cpu,%mem
%CPU %MEM
80.3  0.0

Angesichts des obigen Ergebnisses hätte ich gerne eine CSV-Datei wie diese:

aaa, 80,3, 0,0

während es in einem Shell-Skript wie diesem verwendet wird:

for i in { 1..31 }
do
  aaa=$i
  // call ps and write result to a csv
done

Antwort1

#!/bin/bash

for aaa in {1..31} ;do
  echo $aaa$(ps --no-headers -p "$aaa" -o %cpu,%mem | tr -s ' ' ',') #>>/tmp/output.csv
done

Antwort2

Eine mögliche Lösung – ich glaube, das ist etwas eleganter (und wichtig: Ich habe es (kurz) getestet, um sicherzustellen, dass es wirklich funktioniert).

#! /bin/bash

# This allows you to specify the PID you are interested in at the command prompt, as I expect hard coding is not a good idea.

if [ "$1." == "." ]
then
    echo Usage: $0 pid
    exit 1
fi

for each in {1..31}
do
    echo ${each}$(ps -p $1 --noheaders -o %cpu,%mem| tr -s ' ' ',') >> /tmp/file.name
done

verwandte Informationen