Basename in subshell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Basename in subshell
# 1  
Old 05-06-2012
Basename in subshell

Hi All,

I would like to improve my bash scripting skill and found a problem which I do not understand. Task is to search and print files in directory (and subdirecories) which contains its own name. Files can have spaces in name.

This one works fine for files in main directory, but not for files placed in subdirectories:
Code:
find . -type f -exec sh -c "echo {} | sed s/'\.\/'/''/g" \; | xargs -I'{}' grep -H {} {} | cut -f1 -d:

So I tried to add basename in subshell and it is not working as I expected. There is no result. Path is printed like before.

Code:
[ok@x60 test]$ find . -type f -exec sh -c "echo {} | sed s/'\.\/'/''/g" \; | xargs -I'{}' echo grep -H `basename {}` "{}"
grep -H olooo olooo
grep -H 1 ib_iser 1 ib_iser
grep -H ala ma/00.00 kintegrityd ala ma/00.00 kintegrityd

When I changed basename to dirname to check what happens, I got:
Code:
[ok@x60 test]$ find . -type f -exec sh -c "echo {} | sed s/'\.\/'/''/g" \; | xargs -I'{}' echo grep -H `dirname {}` "{}"
grep -H . olooo
grep -H . 1 ib_iser
grep -H . ala ma/00.00 kintegrityd


So, why basename and dirname act like this? Thank you for any help on this Smilie
# 2  
Old 05-06-2012
If I understand what you are trying to do, this is the way I would have approached it:

Code:
find . -type f| while read f
do
    grep -l "${f##*/}" "$f"
done



Searches all regular files in all directories in and below the current directory and prints files whose contents contain the filename. Should work with file names that have spaces.
# 3  
Old 05-06-2012
Thank you for reply. I think your solution is best possible Smilie But I have been thinking how to resolve this matter without for or while loop. Only for test purposes. This task is not so important. I'm just curious Smilie
# 4  
Old 05-06-2012
Quote:
Originally Posted by new_item
Thank you for reply. I think your solution is best possible Smilie But I have been thinking how to resolve this matter without for or while loop. Only for test purposes.
I don't see a way, because the string being grepped for really is constantly changing. You could do so inside awk I suppose, but it would just mean moving the while loop inside awk instead of the shell.
# 5  
Old 05-06-2012
It may look strange, but I believe that everything works Smilie

Code:
find . -type f -print | awk -F'/' '{print "grep -H \"" $NF "\" \"" $0 "\""}'  | bash | cut -f1 -d:

Corona688: thank you for tip about awk.

Last edited by new_item; 05-06-2012 at 03:49 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How will these subshell commands behave?

Hello, I am firing off some scripts from a main script, cd B/ ./EV_B_m0-m200_hex1.sh & ./EV_B_m0-m200_hex2.sh & wait ...more It would be useful to put a bit of time between the two to clean up the output to the terminal. I think this would work, cd B/ ... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

2. Shell Programming and Scripting

Executing a command in subshell

Hi All, I am trying to create a shell script which runs on my linux machine. The requirement is so that, ade createview xyz -> this steps creates a view ade useview xyz -> we are entering inside the view ade begintrans -> begin a transaction My script has following code : lets say... (2 Replies)
Discussion started by: Dish
2 Replies

3. Shell Programming and Scripting

Getopts in the subshell of ksh

Hi, the getopts doesnt seem to be working in the subshell of the ksh. when I echo $@ and $* from the subshell it shows nothing. even when I am capturing the parameters from the outer shell and passing while invoking the file then I am still not getting it properly. the below code is in the... (9 Replies)
Discussion started by: hitmansilentass
9 Replies

4. Shell Programming and Scripting

While loop subshell problem

People, Here is my code while read ln do xyz=$(echo $ln/$val1*100-100|bc -l|xargs printf "%1.0f\n") if && ; then iam="YELLOW" fi done <<< "$(grep "^" $TMPOUT)" where $TMPOUT is a flat file which contains a set of values. Whilst executing the above, I get an error... (4 Replies)
Discussion started by: sathyaonnuix
4 Replies

5. Shell Programming and Scripting

$(basename $0)

what is the meaning of "script_name=$(basename $0)", can someone please explain? (1 Reply)
Discussion started by: abhi200389
1 Replies

6. Shell Programming and Scripting

getopts in a subshell

Hello, I've a little problem with one of my ksh scripts and I manage to narrow it to the script here: #!/bin/ksh writeLog() { paramHandle="unknown" OPTIND=1 while getopts :i: option $* do case $option in i) paramHandle=${OPTARG} ;; esac done echo... (2 Replies)
Discussion started by: Dahu
2 Replies

7. Shell Programming and Scripting

Killing a subshell

I am calling a script from with another script and reading its output one line at a time (using <childscript> | while read line) in the parent script. If the output exceeds a predefined number of lines I want to kill the child shell from within the parent shell. I decided to print the process ID... (2 Replies)
Discussion started by: slash_blog
2 Replies

8. UNIX for Dummies Questions & Answers

basename

Hi, can anyone let me know how to interpret the below third line in the following code. Gone through the man pages of "basename", but no go. for f in *.foo; do base=`basename $f .foo` mv $f $base.bar done Thanks. (2 Replies)
Discussion started by: venkatesht
2 Replies

9. Shell Programming and Scripting

basename $0

hi, can anyone help me by saying what is basename.. i have seen this in many programs where the basename is used.... thanks, Krips (4 Replies)
Discussion started by: kripssmart
4 Replies

10. Shell Programming and Scripting

Subshell Question

The profile of the user is empty. Then before I run the script I want I run a parameter file that populates the variables for oracle. ORACLE_HOME ORACLE_BASE ORACLE_SID PATH etc ... But it seems that these variables are not making it to the shell I am in because when I do an echo on... (6 Replies)
Discussion started by: lesstjm
6 Replies
Login or Register to Ask a Question