GREP COMMAND IS NOT RESPONDING AGAINST EXTREMELY SMALL AMOUNT OF DATA

 As a sysadmin we run grep command innumerable number of times every day. Today I got to search for some string in a directory. As usual I fired grep command. The command is similar to grep -R hello * . I didn’t get result immediately. Assumed that size of the directory was huge. Instead of checking further, I grabbed cup of tea and was waiting for results. While I emptying the tea cup, I was hoping that empty screen will be filled up with results. Was I too optimistic or finding reason for getting additional cup of tea? But it didn’t happen. So, I cancelled the command and checked the size of the directory. Surprisingly size was few MBs.

So, it was time to pull out one my favorite debugging tools: strace. Fired the command as below:
strace grep -R hello *

Condensed output:

execve(“/usr/bin/grep”, [“grep”, “-R”, “hello”, “-“, “1.sh”, “1.txt”, “1.yaml”, “2.txt”, “2.yaml”, “3.txt”, “cookies.txt”,, “deck_1.9.0_linux_amd64.tar.gz”, “dummy-key.pem”, …], 0x7ffe9be4ac08 /* 24 vars */) = 0
brk(NULL) = 0x1aed000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2a7433c000
access(“/etc/ld.so.preload”, R_OK) = -1 ENOENT (No such file or directory)
open(“/etc/ld.so.cache”, O_RDONLY|O_CLOEXEC) = 3
…. …. ….
ioctl(0, TCGETS, {B38400 opost isig icanon echo …}) = 0
read(0,

The lines in bold are important. Last line tells us that command was trying to read file with file descriptor number 0 which is nothing but standard input. Why command was waiting for user input? Answer lies in the first bolded line. When we use * (globe) in shell, shell will expand to all files matching the pattern. In this case, all the files in the directory. If you observe expanded grep arguments carefully, you can fine “-” in the list of input files. So, thought there is some relationship to “-” and reading from input stream. Fired man grep command and searched for hyphen to check further.

grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the
matching lines.


So, grep is working as expected as it got “-” as input parameter. But why * expanded to hyphen? was there any file or dirctory with name “-“? fFired ls command and found a directory with name “-“. Not sure how it got created. Removed it using command: rm -r — “-“, need to add — otherwise – is treated as option. Issue resolved.

P.S: For ages, I have been executing grep command without file argument (grep Hello) that would caused grep to hang. Later I would realize my mistake and retype the correct command with file name. I never thought about why grep would hang without filename, the answer lies in the above excerpt from man page. Basically, grep is not hanging it is waiting for user input.


Comments

Popular posts from this blog

HOW WE REDUCED SOA OSB PROVISIONING FROM 4 DAYS TO 4 HOURS

NOT ABLE TO START RABBITMQ CLUSTER: CANNOT DECLARE A QUEUE ‘~S’ ON NODE ‘~S’: ~255P

SOA SUITE 12.2.1.4 INSTALLATION: GOT EXCEPTION WHEN AUTO CONFIGURING THE SCHEMA COMPONENT(S) WITH DATA OBTAINED FROM SHADOW TABLE