Script creates additional file


 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Script creates additional file
# 1  
Old 07-20-2017
Script creates additional file

Hi,

I have created a test script like this :

Code:
[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" :

Code:
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?
# 2  
Old 07-20-2017
Hello anaigini45,

Could you please do following change into your script, it code should fly then.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 07-20-2017
This is the mistake:
Code:
if [ "$DAY" > 15 ];then

Fix:
Code:
if [ "$DAY" -gt 15 ];then

--
A suggestion for efficient handling of "date to variables":
Code:
eval $(date "+DAY=%d MONTH=%b YEAR=%Y")

fills three shell variables in one stroke.
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 07-20-2017
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:-
Code:
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
This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 07-20-2017
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 :

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

Smilie

Last edited by rbatte1; 07-21-2017 at 08:46 AM..
# 6  
Old 07-20-2017
[[ ]] and [ ] work similar, but the implementation is different.
Code:
$ 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").
These 2 Users Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Writing a script that creates a 1GB file with zeros using dd

I am new to Linux. Using latest version of Ubuntu. I want to make a script that creates a 1GB file filled with zeros using dd and then formats the file as vfat with a label of "MYFILE". If anyone can help me it would be appreciated. (1 Reply)
Discussion started by: amandasaza08
1 Replies

2. Shell Programming and Scripting

In the script I get additional file, which I don't expect do get

I have the following script #!/bin/sh Usage () { echo "Usage: $0 <config_file>" echo "Example: ./sftp_ondemand_daily.sh /export/data/mbsesb/config/ond emand.cfg /export/data/mbsesb/config/filename.lst" exit 1 } if then Usage fi ... (6 Replies)
Discussion started by: digioleg54
6 Replies

3. Shell Programming and Scripting

Writing a script that creates a 1GB file with zeros using dd

I am new to Linux. Using latest version of Ubuntu. I want to make a script that creates a 1GB file filled with zeros using dd and then formats the file as vfat with a label of "MYFILE". If anyone can help me it would be appreciated. (9 Replies)
Discussion started by: paviter619
9 Replies

4. Shell Programming and Scripting

Lock file creates with '?'

Hi, I am trying to create a lock file with the following code but for some reason after file is created it has wrong name "PASP?.lock??" Please let us know how to get rid of these '??' from file name and from where they are coming? #!/bin/ksh... (6 Replies)
Discussion started by: sandy162
6 Replies

5. Shell Programming and Scripting

Extraction of .tar.gz creates additional unwanted directories

I'm working on a project that requires me to compress then relocate directories to a different location based on their last date of modification. After running the script I check to see if it worked, and upon unzipping the tar.gz using I created everything that should be there is. I then performed... (4 Replies)
Discussion started by: jrymer
4 Replies

6. Shell Programming and Scripting

Script that creates directory structure

Hi All, I have a script that does daily checks on my storage environment and is run from an AIX host. The script currently works great but I have been changing and updating bits of it to make it easier for my lesser colleagues to understand :p However now with the updates I have made I... (1 Reply)
Discussion started by: colinwilson1303
1 Replies

7. Shell Programming and Scripting

Script sometimes creates an empty file

I have a script that runs ditto for me, and occasionally (if I exit the script while ditto is running in the background) it will leave an empty file named 0 in the script's directory. The next time I run the script, it generates incorrect data because of this file. I know I can easily insert a... (1 Reply)
Discussion started by: reid
1 Replies

8. Shell Programming and Scripting

Script sometimes creates an empty file

I have a script that runs ditto for me, and occasionally (if I exit the script while ditto is running in the background) it will leave an empty file named 0 in the script's directory. The next time I run the script, it generates incorrect data because of this file. I know I can easily insert a... (1 Reply)
Discussion started by: reid
1 Replies

9. Shell Programming and Scripting

awk script creates empty line

I have the following awk script to which I pass the file > 10 0 0 10 0 0 > 12.997 0 5.71132 12.9098 0.0687626 5.48855 12.7506 0.174324 5.13225 12.5913 0.262662 4.80643 12.4316 0.335652 4.50283 12.2717 0.394598 4.21542 12.1113 0.440399 3.93957 11.9506 0.473646 3.67148 11.7894... (2 Replies)
Discussion started by: kristinu
2 Replies

10. Shell Programming and Scripting

sed in script creates output file ending with '?' (^M)

Hi, I'm trying to use sed within a shell script (bash, running ubuntu). The command works fine from the command line, but when I use it within the script, rather than creating a file with the name I've specified, it creates one that ends with a question mark '?' when you use ls, e.g.... (3 Replies)
Discussion started by: jennykay
3 Replies
Login or Register to Ask a Question