Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I'm new to BASH scripting, having written a basic bash script I now want to consolidate it using a loop but I cant quite figure out how to do ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2012
    Posts
    3

    Post Problem with BASH script.


    Hi, I'm new to BASH scripting, having written a basic bash script I now want to consolidate it using a loop but I cant quite figure out how to do it.

    Im trying to get the first cycle of the loop to use arg's CD000002D92FF528 and test217bbb.txt and then the 2nd cycle to use B8000002D94BCC28 and test217ccc.txt.

    Unfortunately this does the first loop twice then the second loop twice. I have tried running it as an outer and inner loop and this also didn't work?


    wget ***.1***.***.***/data.xml --output-document=test217a.xml


    for arg1 in CD000002D92FF528 B8000002D94BCC28

    do
    awk '/'$arg1'/{c=5;next}c-->0' test217a.xml >> test217a.txt
    awk '/<field key="TempC" value=/ { print $3 }' test217a.txt >> test217b.txt
    sed s/\"//g test217b.txt >> test217bb.txt
    done

    for arg2 in test217bbb.txt test217ccc.txt

    do
    cut -c 7,8,9,10,11 test217bb.txt >> $arg2
    done

    exit 0

    I've tried numerous different ways but can quite figure out how to do it.

    Any ideas greatly appreciated.

    Thanks

    Andy

  2. #2
    Just Joined!
    Join Date
    May 2012
    Posts
    43
    Why are you using a loop if you only want it to execute once?

  3. #3
    Just Joined!
    Join Date
    Jun 2012
    Posts
    3
    Hi I may well be going down the wrong route

    My current program that works is long winded, it does the following 16 times;

    awk '/CD000002D92FF528/{c=5;next}c-->0' swift217.xml >> probe3a.txt
    awk '/<field key="TempC" value=/ { print $3 }' probe3a.txt >> probe3b.txt
    sed s/\"//g probe3b.txt >> probe3bb.txt
    cut -c 7,8,9,10,11 probe3bb.txt >> probe3c.txt

    The only thing that changes each time is the input reference CD000002D92FF528 and the output file probe3c.txt. So I thought by running a loop just to change these two parameters would make the program a lot shorter.

    Ta, Andy

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jun 2012
    Posts
    3
    Hi, I have figured out one way of doing it, there's probably still something better

    #!/bin/bash

    set -ex

    wget ***.**.***.***/data.xml --output-document=test217a.xml

    rm test217c*.txt

    for arg1 in CD000002D92FF528 B8000002D94BCC28

    do
    rm test217b*.txt
    awk '/'$arg1'/{c=5;next}c-->0' test217a.xml >> test217b.txt
    awk '/<field key="TempC" value=/ { print $3 }' test217b.txt >> test217bb.txt
    sed s/\"//g test217bb.txt >> test217bbb.txt

    if [ "$arg1" == CD000002D92FF528 ]
    then
    cut -c 8,9,10,11,12 test217bb.txt >> test217c1.txt

    fi

    if [ "$arg1" == B8000002D94BCC28 ]
    then
    cut -c 8,9,10,11,12 test217bb.txt >> test217c2.txt

    fi
    done
    exit

    This is only reading in two lots of Arg data but I have also tested it on 16 and it seems to work ok.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •