Operating System - HP-UX
1755845 Members
4770 Online
108838 Solutions
New Discussion юеВ

Re: Shell script to edit a file using 'vi'

 
SOLVED
Go to solution
cybermilky
Occasional Contributor

Shell script to edit a file using 'vi'

How do I write a shell script that allow the user to edit a file using vi?
Let's say the script is called viScript. The usage should be viScript [filename].

Before using vi to edit the file, it creates a backup copy first.

The script must accept one or no parameter from the command line. If no parameter is entered, then prompt the user to enter one. If none is entered then exit. If more than one is entered then display a usage message and exit.

When a filename is entered, it must check whether the file exists and has either read-only or read/write permission for the file. An error message appears if the file does not exist or if the user does not have read-only or read/write permission.

If the user has read-only access to the file, a message is displayed to that effect and along with the file contents. This means that the user can display the file only but not edit it.

If the user has read/write access, a copy of the file is made as filename.bak and then call vi to edit the file.
4 REPLIES 4
Michael Tully
Honored Contributor
Solution

Re: Shell script to edit a file using 'vi'

If your using CDE, then it has own text editor based on 'vi'

Alternatively have a look at the HP Porting Centre. There are a number of editing tools, 'EMACS' could be one that your after.

Go to this link and use 'editor' without the quotes in the search.

http://hpux.connect.org.uk/
Anyone for a Mutiny ?
Christian Gebhardt
Honored Contributor

Re: Shell script to edit a file using 'vi'

The script must accept one or no parameter from the command line. If no parameter is entered, then prompt the user to enter one. If none is entered then exit. If more than one is entered then display a usage message and exit.
---
if [ $# -eq 0 ]; then
echo "Please insert filename \c"
read infile
else
infile=$1
fi
---
Before using vi to edit the file, it creates a backup copy first.
---
cp $infile $infile.$USER,`date`
---
When a filename is entered, it must check whether the file exists and has either read-only or read/write permission for the file. An error message appears if the file does not exist or if the user does not have read-only or read/write permission.
---
vi
---

If the user has read-only access to the file, a message is displayed to that effect and along with the file contents. This means that the user can display the file only but not edit it.
---
vi
---
If the user has read/write access, a copy of the file is made as filename.bak and then call vi to edit the file.
---
see above
---

vi does the most things you want to do

Chris
Balaji N
Honored Contributor

Re: Shell script to edit a file using 'vi'

Hi,
if what you need this script is just for taking backups before editing this file, try using emacs.

Other wise, a script could be like this.

[test conditions]
[backup file]
vi $filename


and when the user types :q in vi, the script terminates automatically.
hth
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Mark Ellzey
Valued Contributor

Re: Shell script to edit a file using 'vi'

You may want to keep in mind that this can be a security issue. The user can shell out of vi and do anything they have permission to do.

Just my 2 cents worth...

Mark