For and if example

From Initq
Jump to: navigation, search
  • bash# for file in *; do if grep -q EMAIL $file; then echo $file; fi;done;
#!/bin/sh
 
#This script gives the name of the matching word in all files.
 
for file in *
do
 if grep -q EMAIL $file
 then
   echo $file
 fi
done
 
exit 0

The output will be just file names because grep -q means quiet.

Check if Number is even or odd

Write a script that prints all odd or even numbers. mod % (returns the remainder of an integer division operation).

#!/bin/bash
 
for n in {1..100}
do
 out=$(( $n % 2 ))
  if [ $out -eq 0 ]
 then
   echo "$n is even number"
 else
   echo "$n is odd number"
fi
done;

For example for fsck

for i in `cat /etc/fstab | grep -i sda1 | awk '{print $1}'`; do fsck $i; done
Personal tools