1748003 Members
4772 Online
108757 Solutions
New Discussion юеВ

Re: Challenging script

 
Sandman!
Honored Contributor

Re: Challenging script

Updated script pasted below since the earlier one made an inverse copy of the block of lines that were to be changed. Execute as follows:

# ./change file1

=========================================================
#!/usr/bin/sh

UP=$(echo $1 | awk '{print toupper($0)}')
Usr=$(awk '/^Alias/ {split($2,z,"/");u=z[2]} END {print u}' $2)
LUsr=$(awk '/^Alias/ {split($2,z,"/");l=tolower(z[2])} END {print l}' $2)

ex -s $2 <<-EOF
g/$Usr/ t /ErrorLog logs\/error_log/
/ErrorLog logs\/error_log/+1,$ s;/$Usr/;/\U$1/;g
/ErrorLog logs\/error_log/+1,$ s;/$LUsr/;/\L$1/;g
g/\/$UP\// m /ErrorLog logs\/error_log/
/ErrorLog logs\/error_log/ m $
wq
EOF
=========================================================

hope it helps!
Unix or Linux?
Frequent Advisor

Re: Challenging script

Sandman,

Whenever I try to run that script it gives the following error.

./change: syntax error at line 3: `UP=$' unexpected

Not sure where the syntax error is?
I have copied directly as you wrote it.

Thanks
Sandman!
Honored Contributor

Re: Challenging script

What OS version are you running? Run your script in debug mode and paste the output here for troubleshooting purposes.

# sh -x ./change

I'ave attached the script so that there are no invisible characters inserted into the shell script when you copy it.
Unix or Linux?
Frequent Advisor

Re: Challenging script

Sorry about the delay. I had to modify the script slightly, as the Unix version I am running does not support the new POSIX standard that uses $() command. I used ` ` instead.

Using cat /etc/release for OS version

Solaris 8



$ sh -x ./change joe test2
+ awk + {print toupper($0)}
echo joe
UP=joe
+ awk /^Alias/ {split($2,z,"/");u=z[2]} END {print u} test2
Usr=BOB
+ awk /^Alias/ {split($2,z,"/");l=tolower(z[2])} END {print l} test2
LUsr=BOB
+ ex -s test2
g/BOB/ t /ErrorLog logs\/error_log/
/ErrorLog logs\/error_log/+1,$ s;/BOB/;/\Ujoe/;g
/ErrorLog logs\/error_log/+1,$ s;/BOB/;/\Ljoe/;g
g/\/joe\// m /ErrorLog logs\/error_log/
/ErrorLog logs\/error_log/ m $
wq
Substitute pattern match failed
Unix or Linux?
Frequent Advisor

Re: Challenging script

Actually using:

uname -sr

SunOS 5.8
Sandman!
Honored Contributor

Re: Challenging script

Hmmm...I thought you might not be on HP-UX as the awk command on HP is the new awk (nawk) while in Solaris it is still the old awk. So in order to make it work on Solaris you have to replace all invocations of awk with nawk.

~cheers

Unix or Linux?
Frequent Advisor

Re: Challenging script

Thanks. Seems to work now. Thanks for the tip regarding awk with Solaris.
Unix or Linux?
Frequent Advisor

Re: Challenging script

Sandman,

Could you give an explanation of the following


g/$User/ t /ErrorLog logs\/error_log/
/ErrorLog logs\/error_log/+1,$ s;/$User/;/\U$1/;g
/ErrorLog logs\/error_log/+1,$ s;/$LcaseUser/;/\L$1/;g
/ErrorLog logs\/error_log/ m $