- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Error Trapping in ksh
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2008 11:49 AM
01-10-2008 11:49 AM
We have a 3rd party application to access our DB without our users having access to the unix prompt. We have run across a bug in their app that will allow shell access.
Is their a way to trap a bad CD call in a script or within a given ksh session?
The problem is something like:
cd directory
would return
user>ksh: directory: not found
due to the preceeding "/" being left off of "directory". (I do not have any control over the "/" being left off.)
Thanks,
Ken
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2008 02:34 PM
01-10-2008 02:34 PM
SolutionAssume we have someone elses code *and* they use bare "cd"s:
$ cat cdcheck
echo "Trying to change to $1"
cd $1
echo "Done. Now in $(pwd)"
We can export an alias and function to take over the shell built-in:
$ alias -x cd=mycd
$ function mycd {
unalias cd
echo "Now in cd function"
if [[ ! -d $1 ]]
then
echo "Naughty directory"
else
cd $1
fi
}
$ typeset -xf mycd
$cdcheck /tmp
Trying to change to /tmp
Now in cd function
Done. Now in /tmp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2008 02:40 PM
01-10-2008 02:40 PM
Re: Error Trapping in ksh
Good code assumes nothing and always tests for exceptions. In a shell script:
...
cd ${MYPATH} || { echo "Cannot 'cd' to ${MYPATH}"; exit 1; }
...
...for example, would cause a failure to 'cd' to exit the script with a return code of one after printing a descriptive message.
If you do not hava any control over the presence or absence of the leading '/' to make an absolute path, then you might write a shell-wrapper for the 3rd-party software that first performs a 'cd' into the path that the third-party application will complete.
For instance, if the 3rd-party application is called '/opt/DB/menu' then rename the application code (to, for example, '/opt/DB/menu.realcode') and craft a shell script (wrapper) that looks like:
#!/usr/bin/sh
MYPATH=/dbpath
cd ${MYPATH} || { echo "Cannot 'cd' to ${MYPATH}"; exit 1; }
/opt/DB/menu.realcode
exit $?
The above shell wrapper is named what the 3rd-party application is expected to be named. Thus, it runs providing the environment that you need.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2008 10:01 AM
01-15-2008 10:01 AM
Re: Error Trapping in ksh
Both of these suggestions are good. Since the code is embeded in an external application, there is no way to rename it or modify it, the shell wrapper would not work. (cool stuff though!)
James' tip is more along the lines of what I am looking for. However, when I try to change to a valid directory, I get an error and it doesn't work:
In cd function
In cd function
In cd function
In cd function
ksh: mycd: recursion too deep
If I change to a bad directory, it works correctly. If I remove the "cd $1" or substitute with an echo statement, it will return the echo. Sorry...I'm script challenged.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2008 10:25 AM
01-15-2008 10:25 AM
Re: Error Trapping in ksh
(I think you reversed in your posting your reference to James vs Bob's help, b/c you say James is more useful, but then you obviously are trying to run Bob's script).
echo "Naughty directory"
else
cd $1
fi
}
to refer to the binary for cd explicitly
echo "Naughty directory"
else
/usr/bin/cd $1
fi
}
that might work for you better.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2008 10:27 AM
01-15-2008 10:27 AM
Re: Error Trapping in ksh
...
echo "Naughty directory"
else
unalias cd
cd $1
fi
}
...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2008 10:43 AM
01-15-2008 10:43 AM
Re: Error Trapping in ksh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2008 11:24 AM
01-15-2008 11:24 AM
Re: Error Trapping in ksh
Ok...figured it out. I do not exactly understand why, but this is what fixed it:
'cd' $1 (instead of cd $1)
Thanks for all of the help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2008 01:37 PM
01-15-2008 01:37 PM
Re: Error Trapping in ksh
Glad it is working for you, but agree the best solution is to fix the program or invocation to avoid this. Clever is fun, but usually the wrong answer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2008 02:50 PM
01-15-2008 02:50 PM
Re: Error Trapping in ksh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2008 06:47 PM
01-15-2008 06:47 PM
Re: Error Trapping in ksh
This won't work because it only changes the child process.
>unalias cd; cd $1
You don't need this over kill.
>Ken: but this is what fixed it: 'cd' $1
Naturally, as mentioned by the man page, that works or what I use in my mycd function:
\cd $1
QUOTING: The special meaning of keywords or aliases can be removed by quoting any character of the name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2008 12:32 PM
01-16-2008 12:32 PM
Re: Error Trapping in ksh
Exactly where I wanted it unaliased. In the sub-process only. I didn't want it to undo the alias up higher in the call tree. Scope of the change was immediate only.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2008 03:22 PM
01-16-2008 03:22 PM
Re: Error Trapping in ksh
You really want to narrow the scope to one word by quoting it. Who knows what the scope of unalias is. (You want to use the smallest hammer possible. :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2008 08:04 AM
01-17-2008 08:04 AM
Re: Error Trapping in ksh
But that's not what your comment was about, so I responded to your issue about the alias only changing the child process - which is exactly what I was shooting for when I made the suggestion.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2008 02:15 PM
01-17-2008 02:15 PM
Re: Error Trapping in ksh
I just said unalias was overkill.
>so I responded to your issue about the alias only changing the child process
No, my child process comment was about /usr/bin/cd being useless.