0

My target is to verify if $FILE is a backup file ( backup file ended with xxxx.xx.xx.xx number

Example of backup files

ls /etc/VRTSvcs/conf/config 

  main.cf.17Dec2012.09.10.14
  main.cf.17Dec2012.09.10.23
  main.cf.17Dec2012.09.10.31

Example of not backup file

  main.cf

I write the following ksh script line in order to verify if $FILE ended with xxxx.xx.xx.xx number

.

     [[ ` echo  $FILE  | grep -v  '[0-9]\{4\}\.[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}' `  -eq 0 ]] && echo Not Backup file

I set the

  FILE="main.cf.17Dec2012.09.10.31" 

and I run this line on Solaris/Linux , but I get "Not Backup file" , in spite $FILE ended with 2012.09.10.31

please advice what the problem with my ksh line ( what need to fix ) in order to match the numbers - xxxx.xx.xx.xx

yael
  • 2,433
  • 5
  • 31
  • 43

3 Answers3

3

Just remove the "-eq 0 "

[[ ` echo  $FILE  | grep -v  '[0-9]\{4\}\.[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}' ` ]] && echo Not Backup file
Guru
  • 254
  • 1
  • 2
1

An inline command substitution returns the output of the command, not the return code

  ` echo  $FILE  | grep -v  '[0-9]\{4\}\.[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}' ` 

would return an empty string, not a zero. Therefore this oneliner would not work as expected.

Dima Chubarov
  • 2,316
  • 1
  • 17
  • 28
0

Why can't we use builtin Regexp with BASH/KSH for this? Where FILE1 is your backfile name

[[ "$FILE1" =~ [0-9]{4}\.[0-9]{2}\.[0-9]{2}\.1[0-9]{2} ]] || echo "Not a backup file"

Time/CPU utilization for this zero. Unnecessarily you are using three commands, echo two times and grep once.

jscott
  • 24,484
  • 8
  • 79
  • 100
Surendra Anne
  • 86
  • 1
  • 4