Hi all!
I have a question regarding possibilities to do line counts.
SEARCH_VAR=TEX
rsh $REM_HOST -l $REM_USER "cd $REM_DIR; ls *$SEARCH_VAR* 2> /dev/null" | sort -n | awk 'BEGIN { FS = "-" } ; { print $1"\t"$0 }'
Will produce an output on the screen like this:
483 483-SOME-TEXT-1
643 643-SOME-TEXT-2
692 692-SOME-TEXT-3
723 723-SOME-TEXT-4
For the files I'm searching for. This is exactly what I want I also what I get at the moment. But if I search for something and no files are found it would be nice to have a message like 'No files match the search criteria'. I need both the output on the screen and the file count.
I guess I could do it like this:
First this to get the desired output on the screen:
rsh $REM_HOST -l $REM_USER "cd $REM_DIR; ls *$SEARCH_VAR* 2> /dev/null" | sort -n | awk 'BEGIN { FS = "-" } ; { print $1"\t"$0 }'
Then this to get the count of the files:
LINECOUNT=$(rsh $REM_HOST -l $REM_USER "cd $REM_DIR; ls *$SEARCH_VAR* 2> /dev/null | wc -l")
But that I don't like for two reasons. I have to do two remote shell sessions and also the code gets ugly...
Doing like this will get the file count printed on the screen but I need it in a variable that I can use to compare with later.
rsh $REM_HOST -l $REM_USER "cd $REM_DIR; ls *$SEARCH_VAR* 2> /dev/null" | sort -n | awk 'BEGIN { FS = "-" } ; { print $1"\t"$0 filecount++ } ; END { print filecount }'
Like:
if [[ $FILECOUNT = "0" ]]; then
Hopefully all in one go...
Is this at all possible...? Using awk somehow? Assign the "filecount" in the awk script to a variable?
Oh, this is in Korn shell.
Thanks for any help!