1. Dashboard
  2. Forum
    1. Unerledigte Themen
  3. Mitglieder
    1. Letzte Aktivitäten
    2. Benutzer online
    3. Team-Mitglieder
    4. Trophäen
    5. Mitgliedersuche
  4. Tutorial Bereich
  • Anmelden
  • Registrieren
  • Suche
Dieses Thema
  • Alles
  • Dieses Thema
  • Dieses Forum
  • Seiten
  • Forum
  • Lexikon
  • Erweiterte Suche
  1. Informatik Forum
  2. Webmaster & Internet
  3. Entwicklung

Execute command stored in variable

  • RomanW
  • 23. Juni 2008 um 13:19
  • Unerledigt
  • RomanW
    3
    RomanW
    Mitglied
    Punkte
    65
    Beiträge
    9
    • 23. Juni 2008 um 13:19
    • #1

    I want to merge many log files (values??.tmp) to one file (values.log):

    e.g. cat values1.tmp values2.tmp >> values.log

    I made this:

    Bash
    #!/bin/bash
    prefix="values"
    suffix=".tmp"
    cmd="cat"
    for i in `seq 1 2`;
    do
      name=$prefix$i$suffix
      cmd=$cmd" "$name
    done    
    cmd=$cmd" >> values.log"
    echo $cmd
    Alles anzeigen

    How to execute $cmd at the prompt as typed?

  • Plantschkuh!
    24
    Plantschkuh!
    Mitglied
    Reaktionen
    163
    Punkte
    6.173
    Beiträge
    1.181
    • 23. Juni 2008 um 13:39
    • #2

    This should work:

    Code
    `$cmd`


    (backquotes!) as should this:

    Code
    echo $cmd | xargs

    *plantsch*

  • RomanW
    3
    RomanW
    Mitglied
    Punkte
    65
    Beiträge
    9
    • 23. Juni 2008 um 13:58
    • #3

    Ok, I tried this:

    Bash
    #!/bin/bash
    clear
    prefix="Comps"
    suffix=".tmp"
    cmd="cat"
    for i in `seq 1 2`;
    do
      file=$prefix$i$suffix
      cmd=$cmd" "$file
    done    
    cmd=$cmd" >> Comps_.log"
    #echo $cmd | xargs
    `$cmd`
    Alles anzeigen

    But I get this:

    Code
    cat: >>: No such file or directory
    cat: Comps_.log: No such file or directory
    test.sh: line 13: local: can only be used in a function

    How handling the >> ?

  • Stefan Kögl
    10
    Stefan Kögl
    Mitglied
    Reaktionen
    10
    Punkte
    1.010
    Beiträge
    199
    • 23. Juni 2008 um 14:24
    • #4
    Zitat von RomanW


    How handling the >> ?

    You could try `$cmd >> somefile`

  • Kampi
    27
    Kampi
    Mitglied
    Reaktionen
    193
    Punkte
    7.828
    Beiträge
    1.468
    • 23. Juni 2008 um 14:43
    • #5
    Bash
    #!/bin/bash
    prefix="values"
    suffix=".tmp"
    cmd="cat"
    merged="values.log"
    
    
    rm -f $merged
    for i in `seq 1 2`;
    do
      name=$prefix$i$suffix
      $cmd $name >> $merged
    done
    Alles anzeigen

    [edit]
    or at the command prompt:

    Code
    $ cat values?.tmp > values.log

    [/edit]

    Willfähriges Mitglied des Fefe-Zeitbinder-Botnets und der Open Source Tea Party.

    Einmal editiert, zuletzt von Kampi (23. Juni 2008 um 14:48)

  • RomanW
    3
    RomanW
    Mitglied
    Punkte
    65
    Beiträge
    9
    • 23. Juni 2008 um 15:16
    • #6

    Thanks to Stefan Kögl and Kampi. Both solutions work well!

  • Maximilian Rupp 27. Dezember 2024 um 12:04

    Hat das Thema aus dem Forum Programmieren nach Entwicklung verschoben.

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!

Benutzerkonto erstellen Anmelden

Rechtliches

Impressum

Datenschutzerklärung