View Single Post
  #2   (View Single Post)  
Old 31st December 2015
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,128
Default

Two demo shell scripts that show the problem as well as the solution. The problematic one stringwise_less.sh:
Code:
 #!/bin/sh

#set -o posix

if [ "X$1" = "X" ]; then
   prev_date="20151229"
else
   prev_date="$1"
fi

cur_date=$(date "+%Y-%m-%d")
cur_date=$(date "+%Y%m%d")

show() {
   echo
   echo "Previous date : [$prev_date]"
   echo "Current date  : [$cur_date]"
   cat <<END
-----file list-------------------------------------
$(ls -ltr)
---------------------------------------------------
END
}

if [ "$prev_date" < "$cur_date" ]; then
   show
   echo "Previous date [$prev_date] < current date [$cur_date]"
fi
 
if [ "$prev_date" = "$cur_date" ]; then
   show
   echo "Previous date [$prev_date] = current date [$cur_date]"
fi

 
if [ "$prev_date" > "$cur_date" ]; then
   show
   echo "Previous date [$prev_date] > current date [$cur_date]"
fi
Running the script:
Code:
$ stringwise_less.sh 20141231                              
./stringwise_less.sh[28]: cannot open 20151231: No such file or directory

Previous date : [20141231]
Current date  : [20151231]
-----file list-------------------------------------
total 8
-rwxr--r--  1 adriaan  adriaan  739 Dec 30 23:59 stringwise_less.sh
-rwxr--r--  1 adriaan  adriaan  764 Dec 31 01:24 stringwise_less2.sh
-rw-r--r--  1 adriaan  adriaan    0 Dec 31 02:27 20151231
---------------------------------------------------
Previous date [20141231] > current date [20151231]
$ rm 2015*
$ stringwise_less.sh 20191231 
./stringwise_less.sh[28]: cannot open 20151231: No such file or directory

Previous date : [20191231]
Current date  : [20151231]
-----file list-------------------------------------
total 8
-rwxr--r--  1 adriaan  adriaan  739 Dec 30 23:59 stringwise_less.sh
-rwxr--r--  1 adriaan  adriaan  764 Dec 31 01:24 stringwise_less2.sh
-rw-r--r--  1 adriaan  adriaan    0 Dec 31 02:29 20151231
---------------------------------------------------
Previous date [20191231] > current date [20151231]
$ date           
Thu Dec 31 02:31:58 CET 2015
$ stringwise_less.sh 20151231                              
./stringwise_less.sh[28]: cannot open 20151231: No such file or directory

Previous date : [20151231]
Current date  : [20151231]
-----file list-------------------------------------
total 8
-rwxr--r--  1 adriaan  adriaan  739 Dec 30 23:59 stringwise_less.sh
-rwxr--r--  1 adriaan  adriaan  764 Dec 31 01:24 stringwise_less2.sh
---------------------------------------------------
Previous date [20151231] = current date [20151231]

Previous date : [20151231]
Current date  : [20151231]
-----file list-------------------------------------
total 8
-rwxr--r--  1 adriaan  adriaan  739 Dec 30 23:59 stringwise_less.sh
-rwxr--r--  1 adriaan  adriaan  764 Dec 31 01:24 stringwise_less2.sh
-rw-r--r--  1 adriaan  adriaan    0 Dec 31 02:32 20151231
---------------------------------------------------
Previous date [20151231] > current date [20151231]
$
The second one, named stringwise_less2.sh solves the issue by using /bin/test. A grep(1) of the changes:
Code:
$ grep test stringwise_less2.sh  
if /bin/test "$prev_date" \< "$cur_date" ; then
if /bin/test  "$prev_date" = "$cur_date" ; then
if /bin/test  "$prev_date" \> "$cur_date" ; then
PS: A warning note lifted from the OpenBSD ksh man page:
Code:
Note: A common mistake is to use ``if [ $foo = bar ]'' which
fails if parameter ``foo'' is NULL or unset, if it has embedded
spaces (i.e. IFS characters), or if it is a unary operator like
`!' or `-n'.  Use tests like ``if [ "X$foo" = Xbar ]'' instead.
Attached Files
File Type: sh stringwise_less.sh (739 Bytes, 57 views)
File Type: sh stringwise_less2.sh (764 Bytes, 60 views)
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump

Last edited by J65nko; 31st December 2015 at 01:54 AM.
Reply With Quote