1

I want to modify the IP in two files:

Contents of File1 has this line:

AS400=127.0.0.1

Contents of File2 has this line:

AS400=127.0.0.1

The bash script below will ask me the IP address of the AS400 and at this time only modify one file:

    #!/bin/bash
    # Modify props file - file1.props
echo " Please answer the following question"    
gawk -F"=" 'BEGIN{
    printf "Enter AS400 IP: "
    getline as400 <"-"
    file="/usr/local/src/file1.props"
    }
    /as400/ {$0="as400="as400}
    {
    print $0 > "temp2"
    }
    END{
    cmd="mv temp2 "file
    system(cmd)
    }
    ' /usr/local/src/file1.props

How do I tell it to update the exact same IP address I type in to file2 also?

Bonus question... Can anyone take a look at this script and tell me why the file getting edited ends up with a ^M at the end of each line?

Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
alycalhoun
  • 13
  • 2
  • If the input file has DOS carriage returns, they will be copied to the output file; you have nothing to remove them (but also nothing to add them, unless you copy/pasted the script imperfectly). – tripleee Sep 16 '13 at 18:22

3 Answers3

2

Instead of wrapping awk, use temporary files and call mv with system(), you could just use bash as a whole:

#!/bin/bash

[[ BASH_VERSINFO -ge 4 ]] || {
    echo "You need bash version 4.0 to run this script."
    exit 1
}

read -p "Enter AS400 IP: " IP

FILES=("/usr/local/src/file1.props" "/usr/local/src/file2.props")

for F in "${FILES[@]}"; do
    if [[ -f $F ]]; then
        readarray -t LINES < "$F"
        for I in "${!LINES[@]}"; do
            [[ ${LINES[I]} == 'as400='* ]] && LINES[I]="as400=${IP}"
        done
        printf "%s\n" "${LINES[@]}" > "$F"
    else
        echo "File does not exist: $F"
    fi
done

Save it to a script and run bash script.sh.

You can also modify it to accept custom list of files instead. Replace this line

FILES=("/usr/local/src/file1.props" "/usr/local/src/file2.props")

With

FILES=("$@")

Then run the script with like:

bash script.sh "/usr/local/src/file1.props" "/usr/local/src/file2.props"
konsolebox
  • 72,135
  • 12
  • 99
  • 105
0

Actually, the script could be made a lot simpler and more elegant and usable if you did away with the interactive prompt, and used a command-line parameter instead.

#!/bin/sh
case $# in
    1) ;;
    *) echo Usage: $0 '<ip>' >&2; exit 1;;
esac

for f in file1.props file2.props; do
    sed -i 's/.*as400.*'/"as400=$1"/ /usr/local/src/"$f"
done

If your sed doesn't support the -i option, fall back to the temp file gyrations you have in the original script.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • This would work also - however the interactive portion is required, as I need to integrate this into my firstboot script for external clients so they can enter in their individual IPs. – alycalhoun Sep 16 '13 at 18:58
0
#!/bin/bash
read -p "Enter AS400 IP: " ip
sed -i '' "s/\(^as400=\)\(.*\)/\1$ip/" file1 file2

As for your bonus, look at this: '^M' character at end of lines

Community
  • 1
  • 1
aktivb
  • 1,952
  • 2
  • 15
  • 16
  • I tried to execute this, however it was not able to find the files. I did try changing the format of the sed line to point to specific location - but still no go. – alycalhoun Sep 16 '13 at 18:59
  • Maybe you "specific location" wasn't specific enough? `/usr/local/src/file[12].props` should work here if it worked elsewhere. – tripleee Sep 16 '13 at 19:26
  • Exactly what did you execute? – aktivb Sep 17 '13 at 00:06