Skip to main content
clarification
Source Link

First off, that isn't how you pipe something to grep. Also, you may wantit is generally considered better form to separate the grepcommands from the conditional and just check its return codecodes.

Something like the following works perfectly:

command=`ls -l`
for file in "$command"
do
  echo "$file" | grep 'o'
  if [ $? -eq 0 ]; then
    echo "$file"
  fi
done

First off, that isn't how you pipe something to grep. Also, you may want to separate the grep from the conditional and just check its return code.

Something like the following works perfectly:

command=`ls -l`
for file in "$command"
do
  echo "$file" | grep 'o'
  if [ $? -eq 0 ]; then
    echo "$file"
  fi
done

First off, that isn't how you pipe something to grep. Also, it is generally considered better form to separate commands from the conditional and just check return codes.

Something like the following works perfectly:

command=`ls -l`
for file in "$command"
do
  echo "$file" | grep 'o'
  if [ $? -eq 0 ]; then
    echo "$file"
  fi
done
You need quotes here. Otherwise, $file will also take the value of the permission strings, the groups and owner names etc.
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

First off, that isn't how you pipe something to grep. Also, you may want to separate the grep from the conditional and just check its return code.

Something like the following works perfectly:

command=`ls -l`
for file in $command"$command"
do
  echo $file"$file" | grep 'o'
  if [ $? -eq 0 ]; then
    echo "$file"
  fi
done

First off, that isn't how you pipe something to grep. Also, you may want to separate the grep from the conditional and just check its return code.

Something like the following works perfectly:

command=`ls -l`
for file in $command
do
  echo $file | grep 'o'
  if [ $? -eq 0 ]; then
    echo "$file"
  fi
done

First off, that isn't how you pipe something to grep. Also, you may want to separate the grep from the conditional and just check its return code.

Something like the following works perfectly:

command=`ls -l`
for file in "$command"
do
  echo "$file" | grep 'o'
  if [ $? -eq 0 ]; then
    echo "$file"
  fi
done
Source Link

First off, that isn't how you pipe something to grep. Also, you may want to separate the grep from the conditional and just check its return code.

Something like the following works perfectly:

command=`ls -l`
for file in $command
do
  echo $file | grep 'o'
  if [ $? -eq 0 ]; then
    echo "$file"
  fi
done