Bash Questions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Questions
# 1  
Old 07-23-2010
Bash Questions

I am writing a Bash script that needs to get part of the current directory path. Let's say the current directory is /cat/dog/bird/mouse/ant. I know that the part that I want is between "bird/" and "/ant". In this case, I would want to set a variable to "mouse".

If the current directory were /cat/dog/bird/giraffe/ant I would want to set a variable to "giraffe" because I want what is between "bird/" and "/ant".

I know that "bird/" and "/ant" are part of the path, but I can't alway predict what will come before that. So, regardless of whether the current directory is /cat/dog/bird/giraffe/ant or /rat/bird/giraffe/ant I want to get the "giraffe" part.

I know I can capture the current directory in my script by doing this: currdir=`pwd`
But I'm not sure what to do afterwards.

I know there is a "cut" command, but that typically works on files, not variables. And I don't know which field number to ask cut to return, because it will vary depending upon what the current directory is.

Would I use awk for this?

Is it frowned upon to use awk within a Bash script? I hesitate to use awk because it seems cleaner to use native Bash rather than embed another whole language in my scripts. What is the conventional wisdom about this? Should I use other languages like awk and Perl in a Bash script? Shouldn't I pick one scripting language and stick to it?
# 2  
Old 07-23-2010
many ways to skin that cat:
Code:
echo '/cat/dog/bird/giraffe/ant' | sed 's#.*/bird/\([^/][^/]*\)/ant.*#\1#g'

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 07-23-2010
Wow. You did sed on a string by just piping echo through it. I didn't know you could do that. I thought sed was for files only.

How do I figure out what the s# and [^/] and 1#g mean? Is that regular expression syntax?
# 4  
Old 07-23-2010
Code:
#.*/bird/\([^/][^/]*\)/ant.*#

.*/bird/         - anything followed by '/bird/'
\([^/][^/]*\) - followed a FIRST 'capture' of: anything, but '/' repeated 0 or more times
/and.*          - followed by '/ant' and anything '.*'


#\1#g
\1                 - substitute the above pattern with the FIRST capture
g                  - do it Globally (g) for all matching pattern on a single line


#\1#g'

read 'man sed' and this link.
This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 07-23-2010
Using built-in bash parameter expansion:
Code:
d='/cat/dog/bird/giraffe/ant'; d=${d#*bird/}; echo ${d%/ant*}
giraffe

See man bash -> 'Parameter Expansion' for more infos
This User Gave Thanks to tukuyomi For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file. Because I'm actually a windows guy and new here so for illustration is sort of : $ bash "echo ${PATH} & echo have a nice day!" will do output, for example:... (4 Replies)
Discussion started by: abdulbadii
4 Replies

3. Shell Programming and Scripting

Bash to select text and apply it to a selected file in bash

In the bash below I am asking the user for a panel and reading that into bed. Then asking the user for a file and reading that into file1.Is the grep in bold the correct way to apply the selected panel to the file? I am getting a syntax error. Thank you :) ... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

Technical questions on bash,mysql and pHp

1. bash -bash escape rules, esp. ',", -how to use Ctrl+R reverse cmd search with regex? 2. mysql -how to use grep in mysql 3. php -why !0 is not evaluated to true? what's its value -php getopt: what if there is duplicate in cmdline args (2 Replies)
Discussion started by: John_Peter
2 Replies

5. Homework & Coursework Questions

Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory. 2.) The folder should have a set-up,phase and display files I have written a small script which i used to check for the existing users and their password. What I need help with: I have a set of questions in a... (19 Replies)
Discussion started by: moraks007
19 Replies

6. Shell Programming and Scripting

Bash Questions

Hello I have to do a program in Bash, need help because it does not go out for me and go enough time with this!! Five directories(boards of directors) that more occupy, arranged according to size. To measure the size of every directory(board of directors) there must not be included the size of... (2 Replies)
Discussion started by: danihj
2 Replies

7. Shell Programming and Scripting

Bash script questions for gurus

guys, I need a steer in the right direction for this issue. it would be great if anyone of you can help me out. i have a textfile where i want to swap the lines based on the user input. The textfile is looks like the #file 1 name TB #file 1 ID 1000 # #file for ID1 system1... (3 Replies)
Discussion started by: mayi
3 Replies

8. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

9. Shell Programming and Scripting

bash and Perl interaction questions

hi. i´m working in bash and am trying to create a Perl daemon that controls bash´s behavior. this is actually in preparation for a later project i´ll be working on. basically, i´m looking for a way to have the Perl daemon tell bash what to do. i already have a small daemon that simply prints... (2 Replies)
Discussion started by: deryk
2 Replies
Login or Register to Ask a Question