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

    • Bad Wolf Theory
    • That sharp cold toothache you dread? Its origins trace back to ancient, unexpected purpose by Sayan Sen Image by Pavel Danilyuk via Pexels Scientists at the University of Chicago have discovered that the sensitive tissue inside our teeth first evolved as part of the armored skin of ancient fish. Their new study, published in Nature, confirms that dentine—a key part of teeth—was originally used by early vertebrates to sense their surroundings. This research supports the idea that dentine wasn’t always used for chewing. Instead, millions of years ago, it helped fish detect changes in the water. The study also clears up confusion about Anatolepis heintzi, a fossil once thought to be the earliest known vertebrate because of its dentine-like structures. For years, scientists debated whether Anatolepis was really an early vertebrate. The fossil had tiny tubules that some researchers believed were odontodes—small structures considered to be the ancestors of teeth. However, there wasn’t enough evidence to be sure. To settle the debate, scientists used synchrotron scanning, a powerful imaging technique, to study different fossils and modern creatures. The scans revealed that Anatolepis didn’t have dentine. Instead, its tubules were sensory structures similar to those found in arthropods like crabs and shrimp. These structures, called sensilla, help animals detect their surroundings. “This shows us that ‘teeth’ can also be sensory even when they’re not in the mouth,” said lead researcher Yara Haridy, PhD. “There’s sensitive armor in these fish. There’s sensitive armor in these arthropods. This explains the confusion with these early Cambrian animals.” Although Anatolepis turned out to be an arthropod, researchers did find real dentine in another ancient fish. The Ordovician vertebrate Eriptychius, which lived about 465 million years ago, had large dentine-filled tubules in its armor. This confirms that dentine first evolved in vertebrates as a sensory tissue. Further tests showed that modern fish, such as sharks and teleosts, still have nerve connections in their external dentine structures. This means early vertebrates may have used dentine to sense their environment before it became part of teeth. Scientists have two main ideas about how teeth came to be. The “inside-out” theory suggests teeth evolved first and were later adapted for exoskeletons. The new research supports the “outside-in” theory, which argues that sensory structures appeared first in exoskeletons and later evolved into teeth. While the team didn’t find the oldest vertebrate fish, study co-author Neil Shubin, PhD, believes the discovery is still important. “We didn’t find the earliest one, but in some ways, we found something way cooler,” he said. Source: University of Chicago, Nature This article was generated with some help from AI and reviewed by an editor. Under Section 107 of the Copyright Act 1976, this material is used for the purpose of news reporting. Fair use is a use permitted by copyright statute that might otherwise be infringing.
    • "How dare you profit off our user's data without compensating them. That's our job!"
  • Recent Achievements

    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
    • Rookie
      Pat-Garrett went up a rank
      Rookie
  • Popular Contributors

    1. 1
      +primortal
      346
    2. 2
      snowy owl
      167
    3. 3
      +FloatingFatMan
      163
    4. 4
      ATLien_0
      161
    5. 5
      Xenon
      128
  • Tell a friend

    Love Neowin? Tell a friend!