how to retain the eol (“\n”) in bash variable or ssh output
Hi, I have to save the result of ssh/grep into a file to keep the eol ("/n"):
ssh $SSH_OPTIONS $USER@$NODE "cd $LOG_DIR; grep -h '$pattern' log.*" > $file So that when I grep on the local file again later, it can be printed out with original log lines. Otherwise, the log lines will be dropped and lines becomes concatenated into a single line, e.g., if I rewrite the script in this way, echoing the $result is not a good idea.. result=`ssh $SSH_OPTIONS $USER@$NODES "cd $LOG_DIR; grep -h '$pattern' log.*"` So question: is there some workaround that I can save it to a variable rather than file but still keep the eol? That will simplify my script and don't need to do all those I/Os! Thanks in advance and happy MLK day! |
Using a temporary file is a pretty good way to go about this sort of thing. Putting log output into a variable is potentially problematic in shell scripting as there is a pretty small limit to the amount of data a variable can hold, and log output can get pretty big - especially when there are problems which you're trying to diagnose... at that time you really don't want to be debugging your shellscript!
Since most modern distros mount /tmp as a ramdisk, there's not really much overhead for something like this: Code:
#!/bin/bash |
Another option might be a pipe, in case you want e.g. to parse the output for some special strings.
|
Quote:
Hence, you can simply do Code:
echo "$result" |
All times are GMT -5. The time now is 05:43 PM. |