Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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
share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.