Sponsored Content
Full Discussion: Basename in subshell
Top Forums Shell Programming and Scripting Basename in subshell Post 302635767 by new_item on Sunday 6th of May 2012 07:31:29 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ZGREP(1)						      General Commands Manual							  ZGREP(1)

NAME
zgrep - search possibly compressed files for a regular expression SYNOPSIS
zgrep [ grep_options ] [ -e ] pattern filename... DESCRIPTION
Zgrep invokes grep on compressed or gzipped files. These grep options will cause zgrep to terminate with an error code: (-[drRzZ]|--di*|--exc*|--inc*|--rec*|--nu*). All other options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If the GREP environment variable is set, zgrep uses it as the grep program to be invoked. EXIT CODE
2 - An option that is not supported was specified. AUTHOR
Charles Levert (charles@comm.polymtl.ca) SEE ALSO
grep(1), gzexe(1), gzip(1), zdiff(1), zforce(1), zmore(1), znew(1) ZGREP(1)
All times are GMT -4. The time now is 10:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy