I have about 350 programs in which I have to add 2 lines; one before and one after a specfic line.
The following script does the job except that I lose the indentation.
#!/usr/bin/bash
while read line
do
line1=$line
if [ "${line1:0:4}" = "@1,0" ]
then
echo "if file (\"/u2/email/eps/\"+alltrim(writer)+\".eps\")"
echo "$line"
echo "endif"
else
echo "$line"
fi
done
I assume the setting of line1=$line is for compatibility reasons for within the existing scripts?
You keep using $line though...
If the setting of line1 is required, i'd put some qoutes around it.
What idention do you loose?
You check for the line/1 to begin with "@1,0" - so there is no idention.
---------- Post updated at 22:45 ---------- Previous update was at 22:40 ----------
Figured, you probably ment to ident the $line in the output, change to
I moved $line to $line1 so that any leading white space would be removed.
I have tried changing IFS to a tilde, but it makes no difference. The programs have indented code for readability
The assignment line1=$line does the assignment, no other treatment.
You need IFS="" read , environment only for read (inheritance from outside the loop does not work for some reason).
Further, read pastes lines together that end with \ .
So often you find
# bash --version
GNU bash, version 3.2.17(1)-release (i586-pc-sysv5)
Copyright (C) 2005 Free Software Foundation, Inc.
#
Running on SCO Openserver 6.0.0 in case it makes any difference.
---------- Post updated at 05:45 PM ---------- Previous update was at 05:37 PM ----------
Success.
#!/usr/bin/bash
IFS="~"
while read -r line
do
IFS=
line1=$line
while [ "${line1:0:1}" = " " -o "${line1:0:1}" = " " ]
do
line1=${line1:1}
done
if [ "${line1:0:4}" = "@1,0" ]
then
echo "if file (\"/u2/email/eps/\"+alltrim(writer)+\".eps\")"
echo "$line"
echo "endif"
else
echo "$line"
fi
IS="~"
done
#
Having the following line produced a run time error
IFS="~" while read -r line
# ./chg.bash <d1.prg >/u1/ltrs2/d1.prg
./chg.bash: line 2: while: command not found
./chg.bash: line 3: syntax error near unexpected token `do'
./chg.bash: line 3: `do'
#