LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Changing directories on the fly inside a script (https://www.linuxquestions.org/questions/programming-9/changing-directories-on-the-fly-inside-a-script-4175513584/)

CoreOxide 08-05-2014 10:17 AM

Changing directories on the fly inside a script
 
Hi guys,

I want a user to be able to run my script, which works on a specific file inside a specific directory, from any directory he might be in right now.

Fore example, I
m currently in:

/sr/42018/2014.07.16.16.13.52/extracted.ALEX-DR_RP.KBox1.2014.07.16.16.13.52/files/var/log

In order to run the rest of my script, I need to be in:

/sr/42018/2014.07.16.16.13.52/extracted.ALEX-DR_RP.KBox1.2014.07.16.16.13.52/files/home/kos/replication


Now, I don't know the starting directory.

How can I force it always to /replication?


Thanks!

szboardstretcher 08-05-2014 10:23 AM

Quote:

Now, I don't know the starting directory.
What do you mean by this?

schneidz 08-05-2014 10:25 AM

to know the currnet directory you can run pwd.

to change directories you can use cd.

CoreOxide 08-05-2014 11:04 AM

Hey again,

I think I didn't explain myself good enough.

I work with logs, and every log I extract goes to a dir named extracted<customername><date><time>

Now, all I know is that starting from extracted* I have my log file structure. What's before that I don't know (the path before extracted*) and I don't know in which current dir I am.

The file I want to work on is in /replication directory, which I want to parse. But I don't know from which dir the user runs the script, so I want it to navigate to / replication, no matter from where it's run AFTER the user has entered some log collection (meaning he's already in /extracted* ).


I was thinking using while to check, something like:

while [ pwd | grep extracted ]
do
cd ..
done


And then when I'm in the directory: /sr/42018/2014.07.16.16.13.52/extracted.ALEX-DR_RP.KBox1.2014.07.16.16.13.52, which is the log structure always start from, I can just cd to /file/home/kos/replication.


I wanted to see if there is a more elegant solution.

Firerat 08-05-2014 11:32 AM

Code:

echo "$PWD"
cd /path/to/somedir
echo "$PWD"
cd -
echo "$PWD"

Or
Code:

echo "$PWD"
pushd /path/to/somedir
echo "$PWD"
pushd /path/to/someotherdir
echo "$PWD"
popd
echo "$PWD"
popd
echo "$PWD"


just some basic examples

ntubski 08-05-2014 12:08 PM

Quote:

Originally Posted by CoreOxide (Post 5215580)
The file I want to work on is in /replication directory, which I want to parse.

I think the best thing is not to change directories at all; just refer to the file by absolute path.

CoreOxide 08-05-2014 01:22 PM

Quote:

Originally Posted by Firerat (Post 5215597)
Code:

echo "$PWD"
cd /path/to/somedir
echo "$PWD"
cd -
echo "$PWD"

Or
Code:

echo "$PWD"
pushd /path/to/somedir
echo "$PWD"
pushd /path/to/someotherdir
echo "$PWD"
popd
echo "$PWD"
popd
echo "$PWD"


just some basic examples


Thanks Firerat,but I didn't quite get how that works... this is one of my first scrips :(
What path do I put instead of "/path/to/somedir"?
The first constant in path is the word "extracted". All the folders between it and /sr/ can be random.



Quote:

Originally Posted by ntubski (Post 5215610)
I think the best thing is not to change directories at all; just refer to the file by absolute path.

Same exact issue as above - the absolute path is dependent on the log directory I'm currently in.



Let's take the path:

Code:

/sr/42018/2014.07.16.16.13.52/extracted.ALEX-DR_RP.KBox1.2014.07.16.16.13.52/files/var/log
Can I grep \ sed somehow that the output will be:

Code:

/sr/42018/2014.07.16.16.13.52/extracted.ALEX-DR_RP.KBox1.2014.07.16.16.13.52
Again, the path can also be like:

Code:

/sr/45689/new_logs/blabla/extracted123123/home/kos
So I would need the grep to output just up to:

Code:

/sr/45689/new_logs/extracted123123/
Meaning, I need to end up at extracted* dir.

ntubski 08-05-2014 01:58 PM

Code:

$ x=/sr/42018/2014.07.16.16.13.52/extracted.ALEX-DR_RP.KBox1.2014.07.16.16.13.52/files/var/log
$ echo $x | sed 's:\(extracted[^/]*\).*$:\1:'
/sr/42018/2014.07.16.16.13.52/extracted.ALEX-DR_RP.KBox1.2014.07.16.16.13.52

So in your script
Code:

extractDir=$(echo "$PWD" | sed 's:\(extracted[^/]*\).*$:\1:')
cd "$extractDir/files/home/kos/replication"
...


Firerat 08-05-2014 04:08 PM

Assuming the directory you want always ends with a digit

Code:

cd "$( grep -Eo "/.*extracted.*[0-9]/" <<<$PWD )/some/extra/subdir"
<<< is heredocs , much better then using echo and pipe, especially if number of processes is limited on busy servers


Some links to help you with bash scripting

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashGuide
http://www.gnu.org/software/bash/manual/bashref.html

The wooledge is especially good, wish i'd known about that much sooner

ntubski 08-05-2014 11:12 PM

Oh right, forgot about grep -o.

Quote:

Assuming the directory you want always ends with a digit
We don't need to assume that though.

Code:

cd "$( grep -Eo '^.*extracted[^/]*' <<<"$PWD" )/some/extra/subdir"

Quote:

<<< is heredocs , much better then using echo and pipe, especially if number of processes is limited on busy servers
You can save even more processes by writing the script in perl, or similar. If you're writing in shell script you've already given up so much efficiency that one or two processes isn't really going to matter; it's only worth trying to save O(n) or more processes.

CoreOxide 08-06-2014 01:57 AM

Thanks ntubski and Firerat.

I used the grep option.

So, for learning purposes:
grep -Eo = enable regex and grep only what I wrote
"/.*extracted.*[0-9]/" = something-extracted-something-number

The only thing that I didn't understand is the triple <<< before $PWD.
I understand logically is gives the grep the output of PWD, but why 3 < ?



Firerat, regarding the links - thanks!
I'm still learning.. that's why I ask questions to actually understand what the grep you gave is doing and not just copy-paste :)

CoreOxide 08-06-2014 09:15 AM

OK, found what's <<< in one of Firerat's links...

So it's the same as:

Code:

"$PWD" | grep -Eo "/.*extracted.*[0-9]/"
Yes?

bigearsbilly 08-07-2014 01:36 AM

I'm sorry,but you are all fired


If the user runs the script as their own user id, then:

cd ~/replication will do it

or work on:

do-stuff-to ~/replication/this-file

Firerat 08-07-2014 03:02 AM

Quote:

Originally Posted by bigearsbilly (Post 5216715)
I'm sorry,but you are all fired


If the user runs the script as their own user id, then:

cd ~/replication will do it

or work on:

do-stuff-to ~/replication/this-file

If a chicken was a ball it would roll.

~/ is home directory, what gives you the idea they need to be in home dir?

ntubski 08-07-2014 10:13 AM

Quote:

Originally Posted by CoreOxide (Post 5216223)
OK, found what's <<< in one of Firerat's links...

So it's the same as:

Code:

"$PWD" | grep -Eo "/.*extracted.*[0-9]/"

Code:

echo "$PWD" | grep -Eo "/.*extracted.*[0-9]/"
Quote:

Originally Posted by bigearsbilly (Post 5216715)
do-stuff-to ~/replication/this-file

Yeah, I thought that too, at first. But it's actually
Code:

do-stuff-to some/function/of/current/dir/replication/this-file


All times are GMT -5. The time now is 06:53 PM.