Problem with PATH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with PATH
# 1  
Old 07-26-2018
Problem with PATH

Recently I lost a number of changes I made to a program when the SCO Unix system went down. The system "mail" suggested a "vi -r" option that took me back several days. To prevent this in the future, I am trying to create my own vi command:
Code:
if [ $# -eq 1 ]
  then
  cp -p $1 $1.bak
fi
/usr/bin/vi $*
if [ $# -eq 1 ]
  then
  rm $1.bak
fi

I put this in my home directory where my PATH in ".profile" is
Code:
PATH=.:/usr/shells:/bin:/etc:/usr/bin:/tcb/bin:/u/wbport

When looking at my home directory from another login, I don't see the .bak file. What am I missing?

------ Post updated at 03:50 PM ------

I could use it after logging off and then back on. If this looks like it would be useful, change the first if to
Code:
if [ $# -eq 1 -a -s $1 ]

If the file doesn't exist yet, there is nothing to back up.

The last if statement should be the same as the first one or just check for the presence of $1.bak .
# 2  
Old 07-26-2018
I'm not sure what you think a PATH variable does. If a file has execute permissions (meaning you can run it by just typing the correct name of the file) then it can be run by typing the whole filename from anywhere

Code:
# example:
/path/to/shell.sh parameter1

This is awkward if you have your current directory somewhere else. So. PATH allows you to put directories into the variable. Then when you type the name no matter where your current directory is set, the PATH looks up the file, then lets you run it.

Example after adding the directory where shell.sh lives to PATH:
Code:
shell.sh parameter1

Not delete it
Not write to it.
Just execute it.

The which command helps you figure out what file you will execute before you try to run it. Why? Well, in the example: shell.sh could be in several places. So if you type shell.sh you could be running another script by accident. PATH can be a cause of this.

Code:
export PATH=".:${PATH}"

makes the current directory part of the PATH. Dangerous.

Example: I use t.shl as a throwaway temporary filename. So it could be in any of the my own directories.
Code:
which t.shl

tells me which one will run when I type the command.

Last edited by jim mcnamara; 07-26-2018 at 10:37 PM..
# 3  
Old 07-27-2018
which is from the csh.
The standard shell has type.
The ksh in SysV introduced whence.

I think the wrapper should cover all arguments
Code:
for f
do
  [ -s "$f" ] && cp "$f" "$f.bak" && echo "created $f.bak"
done
/usr/bin/vi "$@"
for f
do
  [ -f "$f.bak" ] && rm "$f.bak" && echo "removed $f.bak"
done

Unlike $* the "$@" can handle arguments with spaces. Some early shells have a bug, AFAIR the work-around was "${1+$@}"
Code:
vi "file name with spaces"

Last but not least, I think the best backup is built into vi and vi -r is a good recovery.
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 07-27-2018
You can make function to your .profile to overwrite external or builtin command, no need to setup PATH and create scriptfile.

Insert into .profile:
Code:
vi()
{
  for f in $@
  do
     [ -s "$f" ] && cp -f "$f" "$f.bak" && echo "created $f.bak"
  done

  /usr/bin/vi $@

  for f  in $@
  do
    [ -f "$f.bak" ] && rm "$f.bak" && echo "removed $f.bak"
  done
}

# 5  
Old 07-27-2018
$@ muist be in quotes. Unquoted it will expand like $*
Code:
$ cat demo
echo '$@ - word splitting takes place'
for f in $@
do
  echo "$f"
done
echo '"$@" - quotes prevent word splitting; $@ (and [@] in arrays in later shells) split into arguments (elements) nevertheless'
for f in "$@"
do
  echo "$f"
done
$ /bin/sh demo "" 1 2 "3 4"
$@ - word splitting takes place
1
2
3
4
"$@" - quotes prevent word splitting; $@ (and [@] in arrays in later shells split into arguments (elements) nevertheless

1
2
3 4

As a last exercise try
Code:
/bin/sh demo "*"

You see that unquoted the shell does a glob-expansion against the current directory. (That you can prevent with set -f)
# 6  
Old 07-27-2018
Right now my script look like:
Code:
if [ $# -eq 1 -a -s $1 ]
  then
  cp -p $1 $1.bak
echo "While using vi, $1.bak has been created."
sleep 1
fi
/usr/bin/vi $*
if [ -f $1.bak ]
  then
  rm $1.bak
fi

I realize vi can edit a number of files, start at a specific position, or after a search, but we seldom use any extra bells or whistles. Near the end of a Y2K conversion script, a vi searched for "-YEAR" in the command line, but that was 19 years ago.

Thanks for looking at it, I might revisit this thread and check for new ideas in the near future.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

PATH problem

For the sake of not going insane and not buggering a load of needed system stuff, I have created a dir /mybin. (This is a Debian system.) I have then edited the /etc/profile and /etc/login.defs files and added :/mybin to all of the path variables. I have the file /mybin/mtp for... (2 Replies)
Discussion started by: MuntyScrunt
2 Replies

2. Shell Programming and Scripting

Problem with spaces in the path

Hi all, I have a variable test has the following value assigned.. could you please help on doing cd or ls to the value in the varible ... $echo $test /bdm/sdd/compounds/AD4833XT/requests/clin/Watson_20090420/docs/MHRA\ Comments\ \&\ Responses $cd $test ksh: cd: bad argument count $cd... (3 Replies)
Discussion started by: firestar
3 Replies

3. Shell Programming and Scripting

if PATH contains a certain string problem!

Hi I am using MKS Toolkit c shell. I want to basically check if my PATH variable already contains a certain path directory so I tried this (it didnt work!): if: Expression Syntax if ( echo $path |grep -c c:/PROGRA~1/blah/blah ) then please help me get this little statement to work. ... (3 Replies)
Discussion started by: vas28r13
3 Replies

4. Infrastructure Monitoring

Problem with PATH

On one of the machines at work, we had Net-SNMP 5.2.3 installed, and I wanted to upgrade that to 5.4.2.1. So I downloaded the tar file, extracted it, did the configure, make, make test, make install, and everything worked. All the executable files (like snmpget, snmpset, snmpwalk) got copied to... (1 Reply)
Discussion started by: sllinux
1 Replies

5. Solaris

Problem Setting $PATH

I am trying to install the pkg-get package to a fresh install of Solaris 10. I am able to download and install correctly using the default directory for both pkg-get and wget as found on blastwave.org. When I issue the command "which wget and which pkg-get" it returns no wget or pkg-get found in... (1 Reply)
Discussion started by: greengrass
1 Replies

6. Shell Programming and Scripting

Problem with File path

How to pass a file path to open a file? I am using cygwin. I want to open a file from a particular path, say C:\Test\File1,. This file path is stored in a variable. I am able to cat a file like this : cat "c:\Test\File1" but i want the same thing to happen in my script file through a variable... (3 Replies)
Discussion started by: sandeep_hi
3 Replies

7. UNIX for Advanced & Expert Users

path problem

Hi i am writing a script containing processing commands which are reside in /opt/terascan/bin dir. if i run the script from command prompt it is working fine. but in crontab it is not working. if i give env command from command prompt it is showing /opt/terascan/bin dir in PATH variable. ... (10 Replies)
Discussion started by: rajan_ka1
10 Replies

8. Shell Programming and Scripting

path problem

hi , i have written csh script i am unable to set PATH variable in my script. my script is like this ===================================== # ! /bin/csh -f setenv PATH "$PATH:/opt/terascan/bin" ls -l > list lspass > pas peekauto > schedule \ num_days = 1 \ exit 0... (1 Reply)
Discussion started by: rajan_ka1
1 Replies

9. UNIX for Dummies Questions & Answers

cc path problem - no acceptable path found

Hello everyone, I'm a unix noob. I have a powerbook running mac os x 10.4 and for one of my classes I need to install the latest version of php (5.0.5). I'm following the instructions at http://developer.apple.com/internet/opensource/php.html to install but I've run into a problem. The... (2 Replies)
Discussion started by: kendokendokendo
2 Replies

10. Shell Programming and Scripting

PATH variable problem

root->echo $PATH /usr/ccs/bin:/opt/sfw/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin root->find / -name gcc /usr/local/bin/gcc /usr/local/doc/gcc root->which gcc no gcc in /usr/sbin /usr/bin Why cant it find gcc - the path looks okay doesnt it? I am running solaris 9 - and... (7 Replies)
Discussion started by: frustrated1
7 Replies
Login or Register to Ask a Question