You are not logged in.
I'm trying to build a maintenance script and look to have run into a bash intricacy. It seems to run but I think the argument $2 isn't getting put in right.
#!/bin/bash
case $1 in
c | cdf ) shift
cd "$(dirname "$(locate -i "$@" | head -n 1)")"
;;
...
esac
If I run:
cd "$(dirname "$(locate -i firefox/PKG | head -n 1)")"
It runs without a problem. Any ideas?
Last edited by Gen2ly (2009-07-21 09:18:38)
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline
the script gets its own working directory separate from your own. you'll always be 'brought back' to the directory from where you initially ran the script.
if you put `echo "hey look, i\'m in $PWD"` as a line in the script after the case statement you'll see that the _script_ does in fact change directories and you can continue on doing whatever you wanted to do there.
maybe you could export PWD from inside the script? i've never needed functionality like this.
Last edited by brisbin33 (2009-07-18 19:56:48)
//github/
Offline
Export won't work. e.g. (export VALUE=1); echo $VALUE
But if you use your script as a function it would work.
Last edited by Procyon (2009-07-18 20:21:28)
Offline
Ok, I didn't know that before, makes sense though that bash would return to the current working directory. I'm trying:
#!/bin/bash
case $1 in
c | cdf ) shift
zxy () { cd "$(dirname "$(locate -i "$@" | head -n 1)")" ; }
;;
...
esac
But no luck. Are you able to write a function into into a case structure?
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline
The error doesn't lie there. You should debug your program more like brisbin suggested.
--> (saytest() { echo test; }; saytest); saytest
test
zsh: command not found: saytest
Offline
Ok, tested it and that does make alot of sense. Thought it worked but the test was a good idea:
case $1 in
c | cdf ) shift
cd "$(dirname "$(locate -i "$@" | head -n 1)")"
echo "hey look, i'm in $PWD"
cd $PWD
;;
esac
running 'pm-maint c firefox/PKG' will print:
hey look, i'm in /var/abs/extra/firefox
But, as you mentioned, will not be able to export it. As for functions I'm learning these still. I can put:
zxy () { cd "$(dirname "$(locate -i "$@" | head -n 1)")" ; }
in my .bashrc but not sure how to add a function to a case structure. Using it as I have it is a bit odd as I'd have to use:
maint c zxy firefox/PKG
Which doesn't work (tried removing the space too). Pretty new to functions, any help would be appreciated.
Last edited by Gen2ly (2009-07-18 23:22:35)
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline
Well, I looked into this a bit more and it is literally impossible to cd within a bash script. Yeah, it is a sub-shell and cd is built into bash so it's not even possible with a 'find ... -exec cd' command. You can source the file, eg:
. script # or
source script
But obviously that isn't very useful. Best to do as a function in the ~/.bashrc so it's actually in the environment. Thanks for the help.
Last edited by Gen2ly (2009-07-21 09:04:56)
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline