On bash, I would like to pass the search result of "find" to another command as well as to the standard output. The above code performs the same search twice -- once for "xargs -0 chmod" and another for stdout. I would like to spare this redundancy. I would like to search only once and would like to use the search result for another command and reuse it for the standard output.
Assume that the null character (0x00) is the only character that filenames never contain. Thus, assume that filenames may contain newline (0x0A) characters. In the above code, -print0 and -0 options specify the separator to be a null character.
If I allow myself to use a file to save the search result, then the work can be accomplished in the following manner.
However, I do not want to use a file to save the search result. So, I tried a variable to save the search result.
However, the above code failed. It seems that bash removes null characters when bash expands the variable.
I also tried "tee" to make an attempt to split the search result to "xargs -0 chmod" and stdout. The following attempt with "tee" failed.
Can you show me how to use the search result of "find" for another command and reuse it for the standard output without saving the search result to a file? I failed in a method with a variable and a method with "tee".
Thanks Corona688 for reminding me of named pipes. However, the statement "mkfifo mypipe" creates a file "mypipe" in the current directory. I do not want to use any files explicitly. So, instead of Corona688's explicit named pipe, I have ended up with the following implicit named pipe.
The greater-than symbol followed by a subshell is the implicit named pipe. Implicit named pipes do not need "mkfifo" statements. This single-line solution is successful and satisfies my initial demand.
By the way, the purpose of "tr \\0 \\n" is to convert back the null-char-delimited result into newline-delimited data for stdout. I might further modify the tr statement to the following so that possible newline characters in filenames be represented by question marks.
If I need to use (or reuse) the search result later but not immediately following "find", I would like to save it into a variable. However, the search result contains null characters as separators, and bash removes null characters either upon variable assignment or upon variable expansion. To preserve null characters, the percent-encoding is useful.
Percent encoding is widely used for URL encoding. The following method applies percent-encoding to only the null character and the percent symbol itself.
The following example saves the search result of "find" into a variable with percent-encoding.
By the way, does anyone know at which time bash removes null characters, at the time of variable assignment or at the time of variable expansion? Which time? Does bash never save null characters into a variable? Or, does bash save null characters into the variable, and does bash remove null characters when the variable is expanded?
Furthermore, without any encoding (such as percent encoding), is there any option to prevent bash from removing null characters?
I suspect bash uses the standard C string functions to deal with it's env vars, and they use NULL as a terminator. One could imagine that re-implementing a new string object that supported imbedded NULL chars wouldn't be worth the effort.
BTW, the contents of named pipes aren't stored in the Filesystem (the file is only a pointer, much like a /dev/ file, the actual data is stored in memory).
Also, how about using the -e sed param in PercentizeNull() and DepercentizeNull() to reduce the number of sed processes run:
Last edited by Chubler_XL; 03-19-2012 at 06:07 PM..
This User Gave Thanks to Chubler_XL For This Post:
I suspect bash uses the standard C string functions to deal with it's env vars, and they use NULL as a terminator. One could imagine that re-implementing a new string object that supported imbedded NULL chars wouldn't be worth the effort.
Also, how about using the -e sed param in PercentizeNull() and DepercentizeNull() to reduce the number of sed processes run:
Thanks, Chubler_XL.
It was indeed awkward of my code to invoke "sed" consecutively multiple times. The subroutines should be polished as you suggested.
In addition, the following method with semicolon also spares consecutive multiple invocations of "sed".
Hello.
System : opensuse leap 42.3
I have a bash script that build a text file.
I would like the last command doing :
print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt
where :
print_cmd ::= some printing... (1 Reply)
These three finds worked as expected:
$ find . -iname "*.PDF"
$ find . -iname "*.PDF" \( ! -name "*_nobackup.*" \)
$ find . -path "*_nobackup*" -prune -iname "*.PDF"
They all returned the match:
./folder/file.pdf
:b:
This find returned no matches:
$ find . -path "*_nobackup*" -prune... (3 Replies)
Hi All,
I'm completely new to bash scripting and still learning my way through albeit vey slowly.
I need to know where to insert my server names', my ip address numbers through out the script alas to no avail.
I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Hi All,
I am new here but I have a scripting question that I can't seem to figure out with the "find" cmd.
What I am trying to do is to only have to run a single find cmd parsing the directories and output the different file types to induvidual files and I have been running into problems.... (3 Replies)
Hello.
Following recommendations for one of my threads, this is working perfectly :
#!/bin/bash
CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" )
Now I need a grep success for some thing like :
#!/bin/bash
CNT=$( grep -c -e "some text_1... (4 Replies)
I guess by "pattern," I mean something different from how that word is defined in the Linux world. If you take $ to mean a letter (a-z) and # to mean a number (0-9), then the pattern I'm trying to match is as follows:
$$$##-####-###-###.jpg
I'd like to write a script that reads in a list of files... (4 Replies)
I want to use the find command to search a ton of files, but I want to break it up into multiple machines. I want to search for files with "filename." in the title.
The location I want to search is:
/u/*/*/*/stuff
On the first computer I want to search:
/u//*/*/stuff
Right now I am doing... (1 Reply)
Dear friends,
please tell me how to find the files which are existing in the current directory, but it sholud not search in the sub directories..
it is like this,
current directory contains
file1, file2, file3, dir1, dir2
and dir1 conatins
file4, file5
and dir2 contains
file6,... (9 Replies)