Skip to main content
typo, missing IFS= and -r, moved the redirection to a command group so case be not run if the file can't be opened.
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Read a line, using "read", then use "case" to decide what to do.

read{
 First lineIFS= <read File.txt-r Firstline
  case "$Firstline" in
     ("HDR"*"1234"*) mv File.txt Pattern1.txt
                echo "File with pattern1 received..." ;;
     ("HDR"*"5678"*) mv File.txt Pattern2.txt
                echo "File with pattern2 received..." ;;
     (*)        echo "Nothing matched" ;;
  esac
} < File.txt

Read a line, using "read", then use "case" to decide what to do.

read First line < File.txt
case "$Firstline" in
   ("HDR"*"1234"*) mv File.txt Pattern1.txt
              echo "File with pattern1 received..." ;;
   ("HDR"*"5678"*) mv File.txt Pattern2.txt
              echo "File with pattern2 received..." ;;
   (*)        echo "Nothing matched" ;;
esac

Read a line, using "read", then use "case" to decide what to do.

{
  IFS= read -r Firstline
  case "$Firstline" in
     ("HDR"*"1234"*) mv File.txt Pattern1.txt
                echo "File with pattern1 received..." ;;
     ("HDR"*"5678"*) mv File.txt Pattern2.txt
                echo "File with pattern2 received..." ;;
     (*)        echo "Nothing matched" ;;
  esac
} < File.txt
Source Link
icarus
  • 19.1k
  • 1
  • 42
  • 57

Read a line, using "read", then use "case" to decide what to do.

read First line < File.txt
case "$Firstline" in
   ("HDR"*"1234"*) mv File.txt Pattern1.txt
              echo "File with pattern1 received..." ;;
   ("HDR"*"5678"*) mv File.txt Pattern2.txt
              echo "File with pattern2 received..." ;;
   (*)        echo "Nothing matched" ;;
esac