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

3 Answers 3

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
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.

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .