using CD in ksh script


Recommended Posts

hello gents, hoping someone can point me in the right direction, i am tryign to get a simple script to work that will place you into a directory using a variable; here's basicly what my script looks like:

#!/bin/ksh

if [ $# -eq 0 ] ; then

echo ""

echo "Usage: godir {dirname}"

echo ""

exit

else

cd /dir1/adir/thisdir/diriwant/$1

fi

The Usage part works just fine, but of course when it hits the CD command, it give me an error, more or less that "cd" is not found.

Now i've been told the cd command can't be used in a script because its part of the shell, but i would think there is some way to call it so that it responds without placing you back in your home directory or erroring out.

any idea's? :huh:

Link to comment
https://www.neowin.net/forum/topic/305273-using-cd-in-ksh-script/
Share on other sites

You're doing it too hard, just make an alias:

alias lcd='cd /where-ever/you-want/it/$1'

Or whatever you want to call the alias, lcd is just an example :)

And the reason you can't use cd in a script is because the parent process does not gain the environmental path of the child process (the script).

You could go around this by using source but aliasing is tons easier and faster.

As an additional note, I tried this on my Windows box here at work (with MS SFU installed), and it works (minus the fact that daPhoenix pointed out about the parent {your shell} not inheriting the environmnet {current directory} from the child {your godir script}).

Here was my modified script that used my C: drive and pulled a listing from the directory I specified. As the ls command is after the cd, it listed files from the specified directory, as expected.

Not sure why you were having problems with "cd" not being recognized. :unsure:

#!/bin/ksh


if [ $# -eq 0 ]; then
echo ""
echo "Usage: godir {dirname}"
echo ""
exit
else

cd /dev/fs/C/$1
ls

fi

  Quote
You're doing it too hard, just make an alias

urmmm, actually i did -i'm lazy that way :p

but i want this script to work too, i beleive i found the problem however -there were some oddly defined variables in my .kshrc for cd/_cd that kept throwing it off.

thanks for the insight guys

:cool:

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Tesla instructor reportedly said staff leave with a 'negative taste in their mouth' by Hamid Ganji Tesla has been making the headlines over the past few months due to Elon Musk's controversy in the Department of Government Efficiency, aka DOGE. People have been marching to the streets, boycotting Tesla, and even setting their already-bought Tesla cars on fire. Tesla temporarily shut down its factory in Austin for the week of Memorial Day, and employees could either take paid time off or attend a series of training sessions. Business Insider now claims to have obtained a recording of the sessions that reveals some interesting details about the Tesla culture and how its employees feel about the company. The Tesla instructor reportedly asked employees to respond if they ever felt "I can't work under these conditions" and were uneasy about the company's constant change. "I know I have," the instructor said. "A lot of people leave this company, and they have kind of a negative taste in their mouth," the Tesla instructor added. "They think: 'Man, it was terrible. It was bad. I got burnt out. I feel like I didn't get anything done, nobody listened to me.'" Hundreds of Tesla employees allegedly attended the meetings, where they were asked to take more responsibility for improving the company's culture. "Leadership has kind of another level of responsibility for trying to guide and direct that culture," the instructor told Tesla staff. "But at the end of the day, it's us as the people on the ground that are the reflection of the culture." Tesla's factory in Austin produces Cybertruck and Model Y. The staff said shutting down the factory for the sake of Memorial Day has been unusual for the company. Elon Musk recently announced that he would leave his position at the White House and added that he'll remain Tesla CEO for another five years. In the meantime, the latest data shows Tesla sales in Europe have dropped 49 percent, and the company's profit in Q1 2025 declined by 71 percent.
    • More like you took offense to the letdown and I find it hilarious. Most people aren’t reading tech blogs, and for those that do, they know what the difference is. So really it’s a non-story to YOU, because you don’t like what it says.🎻
    • Building 100 hospitals around the world would have spent his fortune easily. But that would not keep him at the table with the elites in the world for the rest of his life. Or keep him from chanting about how good he is to the poor people around the globe to the media.
    • Exactly. that's what I use when I do use Windows 10 on my Linux through QEMU/KVM VM (the 'activation' is tied to the UUID of the VM) and, like you said, that Jan 2032 version of Win10, even if decent program/game support ends earlier than Jan 2032, I suspect, for a conservative figure, that we got at least 2-3 years from now (so call it 2027-2028) before Win10 may start to become more of a legitimate issue. but for general usage I suspect we got a 'easy' 2-3+ years. because like you say, as long as browsers don't start to become a issue, all the more reason to continue using Win10 for many, probably most, people. even when it comes to browsers, you mentioned Firefox supports Win7 until end of August etc... but I think it's just ESR releases which, while I get it technically works, are when browser support is getting a bit long-in-the-tooth. but as long as the standard Firefox release is still supported, then one is still easily well within the 'good support range', which my comment basically plays inline with what Werct said, which I suspect comes back to my 2027-2028 estimate before Win10 'starts' to legitimately fade off into the sunset
  • Recent Achievements

    • Dedicated
      jbatch earned a badge
      Dedicated
    • Week One Done
      Leonard grant earned a badge
      Week One Done
    • One Month Later
      portacnb1 earned a badge
      One Month Later
    • Week One Done
      portacnb1 earned a badge
      Week One Done
    • First Post
      m10d earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      270
    2. 2
      snowy owl
      158
    3. 3
      +FloatingFatMan
      145
    4. 4
      ATLien_0
      141
    5. 5
      Xenon
      131
  • Tell a friend

    Love Neowin? Tell a friend!