You are not logged in.

#1 2009-07-18 19:06:54

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

[ Solved ] Bash script help - shift error?

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

#2 2009-07-18 19:55:52

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [ Solved ] Bash script help - shift error?

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)

Offline

#3 2009-07-18 20:21:18

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [ Solved ] Bash script help - shift error?

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

#4 2009-07-18 21:54:02

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [ Solved ] Bash script help - shift error?

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

#5 2009-07-18 21:59:36

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [ Solved ] Bash script help - shift error?

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

#6 2009-07-18 22:04:54

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [ Solved ] Bash script help - shift error?

your problem could also be

zxy () {

when it should be

zxy() {

no space.

procyon's example is quite illustrative though.

Offline

#7 2009-07-18 23:21:30

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [ Solved ] Bash script help - shift error?

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

#8 2009-07-18 23:25:54

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [ Solved ] Bash script help - shift error?

hmm, maybe if you elaborated a bit on the overall goal of this script we'd be of more help.

Offline

#9 2009-07-21 09:04:04

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [ Solved ] Bash script help - shift error?

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

Board footer

Powered by FluxBB