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

    • My 5090 FE fans were being weird, speeding up and slowing down creating some noise, even after reboot. Updated to these drivers and did this undervolt https://www.reddit.com/r/nvidi..._better_than_stock_at_450w/ (but then to 2800MHz) and it has been fine since.
    • fElon Skum is trash and all his companies and products are just the same.
    • And of course that battery is the thing I'll need to replace sooner or later. It's starting to act a little flaky in its charge meter and that glue is just annoying to deal with. No need for it to be glued in place.
    • I'll say you are 50% correct. On the content side, yes, YouTube's content is basically free. Some might point out that they share ad revenue with creators, but I'll side with you saying that doesn't count. If the ad isn't viewed, the creator gets nothing from YouTube, so viewing the content is net-zero for everyone, not net-negative. The other half of the equation is the platform costs. Both YouTube and Netflix have a cost to provide their service. Netflix bakes the cost into their plans, which can be plainly seen by the different resolutions offered at different tiers, which is a pure platform cost. As you pointed out, YouTube doesn't pay licensing fees, so their entire cost structure is the platform. Using that platform without watching the ads (or paying for premium) is not net-zero like above, in this case it is net-negative. You consumed resources on YouTube systems that YouTube very clearly is not offering for free, they expect compensation in the form of viewing ads or paying for premium. TLDR, the company offering the service sets the price, either in currency or ad viewing. As the consumer, you can choose to make that agreement or not. Consuming the service without paying the price is not being offered.
    • Exactly that, in my case it doesn't work at all!
  • Recent Achievements

    • Week One Done
      mywakehealth earned a badge
      Week One Done
    • 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
  • Popular Contributors

    1. 1
      +primortal
      295
    2. 2
      snowy owl
      163
    3. 3
      +FloatingFatMan
      156
    4. 4
      ATLien_0
      143
    5. 5
      Xenon
      125
  • Tell a friend

    Love Neowin? Tell a friend!