Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

Writing a script to modify text in a file

I need help in writing a script that will modify one word of text in my users .cshrc file.  Here's what the line looks like now:  setenv NNTPSERVER oldserver, and I need to find a way to make it look like setenv NNTPSERVER newserver.  I know I can vi every file to make the necessary changes, but I have over 200 accounts, and that would take a while.  Thanks for any help you can provide.
0
bpb25
Asked:
bpb25
  • 3
  • 3
  • 2
  • +3
1 Solution
 
soupdragonCommented:
Something like

#!/usr/bin/ksh
for USER in `cat /etc/passwd | awk -F: '{ print $1 }'`
do
# cd to each home directory in turn
cd ~$USER
# Make a backup of the cshrc if it exists - just in case
if [ -f .cshrc ]
  cp -p .cshrc .cshrc.save
  # edit it using stream editor
  sed -e 's/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/' .cshrc >.cshrc
fi
done

- Try it out with a couple first before running the script i.e for USER in 'adam joe'

SD
0
 
liddlerCommented:
For best practise, the sed should write to another file, rather than overwriting the original, as sometimes this can 0 the file. so
sed -e 's/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/' .cshrc >.cshrc
should be
sed -e 's/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/' .cshrc >.cshrc.tmp
mv .cshrc.tmp .cshrc
0
 
soupdragonCommented:
True, or even better since you already have a copy of the file
sed -e 's/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/' .cshrc.save >.cshrc
fi

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
bpb25Author Commented:
Soupdragon,

When I try running the script, I get "syntax error at line 13 : 'fi' unexpected.  Any idea's on how to fix this?

Thanks,

Brian
0
 
bpb25Author Commented:
Soupdragon,

When I try running the script, I get "syntax error at line 13 : 'fi' unexpected.  Any idea's on how to fix this?

Thanks,

Brian
0
 
darren1031Commented:
You could also do it this way:

(echo ":%s/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/"; echo ":wq") | vi -s filename

Darren
0
 
interiotCommented:
find /home/ -name .cshrc -print | xargs -n1 perl -i.bak -pe 's/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/'

That will make a .cshrc.bak of the original file in each user's directory.  (which is what the -i option does)
0
 
jwelterCommented:
Without looking myself, and just remembering some of my own scripting, I'm thinking you need a "; then" after the if test condition, for example:

if [ condition test ]; then
command
command
fi

Otherwise, I think if just executes the next command and expects an fi???

JW
0
 
jwelterCommented:
hmmm -I read that too fast.  After looking at the fi problem again, actually I would suggest that the "; then" would still work, but it's not a clear description of that problem.  I think because there is no "; then", it executes the copy, then exits the if automatically, so when it hits the fi after the sed, it's like "hey I don't need you, I'm done with my if."  -Also, if it exits the if after the copy, no matter what, it's going to try and sed/edit the file & save it, so if the file didn't exist, the script would still bomb?

Food for thought...

JW
0
 
soupdragonCommented:
jwelter is right - I forgot my then!
should be

if [[ -f .cshrc ]]; then
0
 
bpb25Author Commented:
This ended up working great, thanks for all who responded.

Something like

#!/usr/bin/ksh
for USER in `cat /etc/passwd | awk -F: '{ print $1 }'`
do
# cd to each home directory in turn
cd ~$USER
# Make a backup of the cshrc if it exists - just in case
if [[ -f .cshrc ]]; then
 cp -p .cshrc .cshrc.save
 # edit it using stream editor
 sed -e 's/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/' .cshrc >.cshrc
fi
done

- Try it out with a couple first before running the script i.e for USER in 'adam joe'

SD
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now