cd command not working through variable in shell script
hi
I am trying to use cd command in shell script like this: script trying to run: directoryName='Test' echo "directory to change "$directoryName cd $directoryName command prompt (screen shot) Administrator@neha /cygdrive/c $ ./file1.sh directory to change:Test : No such file or directoryt /cygdrive/c Administrator@neha /cygdrive/c $ cd Test Administrator@neha /cygdrive/c/Test i have tried many options like cd ${directoryName} cd "$directoryName" but options are also not working Kindly help me out... |
Quote:
Code:
directoryName=Test |
Quote:
Try running it as "source ./file1.sh". I'd also suggest looking at some of the bash scripting tutorials that you can find on the web. |
If those were exact copy-and-paste from your screen then they don't make sense.
BTW, it's easier to read your stuff if you put it between code tags, that's CODE and /CODE in square brackets like []. If you switch to Advanced mode, there's a # icon that automates it. You posted that the script was Code:
directoryName='Test' Code:
Administrator@neha /cygdrive/c Secondly, the script contains cd $directoryName and the output from it was : No such file or directoryt. Here's what I get when trying to cd to a non-existant directory at the prompt Code:
c@CW8:~$ cd kdfghjdhfg Thirdly, where did the /cygdrive/c come from? Is it part of Administrator@neha /cygdrive/c? If those were not exact copy-and-pastes from your system then it is difficult for us to help you! One possibility that might cause the problem is if cd is an alias. You can get the un-aliased cd by escaping it with a "\" as shown below. I've added a couple of lines to the script to help with debugging; please post the output from running this script. Code:
directoryName='Test' |
Avoid tee too
I also noticed if you instinctually added tee it can cause issues.
E.g., cd $DIRECTORY | tee -a $LOGDIRECTORY/out.log causes issues. It won't show an error, but it won't change your directory either. But cd $DIRECTORY works fine. (Logging use of the cd command doesn't buy you much anyway.) |
Quote:
|
All times are GMT -5. The time now is 02:07 AM. |