Script creates additional file

Hi,

I have created a test script like this :

[root@L28tstream1 ~]# cat script1.sh
DAY=$(date +%d)
MONTH=$(date +%b)
YEAR=$(date +%Y)
BC01="Blast_BC01"
BC15="Blast_BC15"
DIR1="$MONTH$YEAR_$BC01"
DIR2="$MONTH$YEAR_$BC07"
DIR3="$MONTH$YEAR_$BC15"

if [ "$DAY" > 15 ];then
        mkdir -p "$YEAR/$DIR3"
fi
[root@L28tstream1 ~]#

When I execute the script, the directories I want is created, i.e :
/root/2017/JulBlast_BC15

However, and additional files is also created. The file name is "15" :

drwxr-xr-x 3 root root    4096 Jul 20 18:06 2017
-rw-r--r-- 1 root root       0 Jul 20 18:06 15
[root@L28tstream1 ~]#

Why does this file get created? And how do I prevent it from being created?

Hello anaigini45,

Could you please do following change into your script, it code should fly then.

cat script1.sh
DAY=$(date +%d)
MONTH=$(date +%b)
YEAR=$(date +%Y)
BC01="Blast_BC01"
BC15="Blast_BC15"
DIR1="$MONTH$YEAR_$BC01"
DIR2="$MONTH$YEAR_$BC07"
DIR3="$MONTH$YEAR_$BC15"
if [[ "$DAY" > 15 ]];then
        mkdir -p "$YEAR/$DIR3"
fi
 

Thanks,
R. Singh

1 Like

This is the mistake:

if [ "$DAY" > 15 ];then

Fix:

if [ "$DAY" -gt 15 ];then

--
A suggestion for efficient handling of "date to variables":

eval $(date "+DAY=%d MONTH=%b YEAR=%Y")

fills three shell variables in one stroke.

1 Like

You haven't said what shell you are using. If this is ksh or plain sh, then the test you are performing in your if statement is still not valid. The double [ and ] suggested are bash. You are saying something like "If the value of $DAY is not null (always true for your code) create or replace the file 15"

You have a redirector, > where you need an expression. Have a look at the manual page for test to see them all. I think you need:-

if [ $DAY -gt 15 ];then

I have removed the quotes from $DAY so it is not necessarily a string to do a numeric comparison, but the issue now is that if $DAY is null or unset, the if statement is again invalid, so you need to be certain it is set to something.

I hope that this helps,
Robin

1 Like

Thank you very much for all the suggestions. All 3 suggestions have worked, it does not generate the "15" file anymore.

But as usual, I have some more questions on the suggestions posted.
RavinderSingh's suggestion on the [[ ]] braces :

if [[ "$DAY" > 15 ]];then

What does the [[ or [ braces mean in the coding language?

On rbatte1's question on which shell I am using, it is bash. Thus I belief this code will work for me.

:slight_smile:

[[ ]] and [ ] work similar, but the implementation is different.

$ type [
[ is a shell builtin
$ type [[
[[ is a shell keyword

The [ is a builtin command. In fact it is an alias of the test command. (When run as [ the last argument must be ] . Command arguments may be redirected with < and > , and that is what caused your problem. The redirection can be prevented by quoting "<" ">" or \< \> , but the standard operators are -lt and -gt . As with all command arguments, you should use "quotes" to prevent word splitting and globbing.
The [[ ]] is a compound, built into the shell interpreter. Because it is not a command, its content can be freely designed, and in fact a < and > are valid integer comparators. And the values are not subject to globbing, and word splitting happens before variable substitution (i.e. you need to quote "two words" to be seen as one value but not $word even if it's value is "two words").

2 Likes