0

I have written the following code using three different ide . The code is simple it creates the database on hive, but the database name will be dynamic.

#!/bin/bash
year=`date +%Y`
month=`date +%m`
day=`date +%d`
echo ${year}
echo ${month}
echo ${day}
dbname=dynamic_hive_${year}_${month}_${day}
echo ${dbname}
hive -e "create database if not exists ${dbname}; use ${dbname}; drop table bins_1; CREATE TABLE bins_1 (firstname string);"

I uploaded this code in our environment and ran it using the java program. The java program uses the java Runtime.exec to run the script. When I see the logs though, the result is as below:

enter image description here

Here I am getting a space in between each echo and the database name is also not as expected. If I manually run the uploaded file doing sh testfile.sh, then I get the following result:

enter image description here

The database is printed but it is so weird and not as expected. In the error we can see the database is not being able to be parsed. I think the new line appears after 2020 so it does not get parsed.

If the same code I copy in my server, by doing vim and run it then it runs absolutely fine. I am so confused and I really don't know what I am missing out. This should be fairly simple but I am missing out something. If someone could help me out, I would really appreciate it.

Saugat Dahal
  • 39
  • 1
  • 10

1 Answers1

1

It looks like you have a problem with line endings. If you check the line-ending in VSCode before copying over - it seems like your script is coming over with CRLF as a delimiter instead of just LF, perhaps?

How to make all line endings (EOLs) in all files in Visual Studio Code, UNIX like?

Also, try doing "cat -vt /path/to/script" to see any odd characters after copying it over.

Ian McGowan
  • 3,461
  • 3
  • 18
  • 23
  • You can also run dos2unix on the script before actually running it. – Matias Barrios May 27 '20 at 18:38
  • Thank you so much Ian McGowan. It seems there odd characters in the file. As @MatiasBarrios had suggested I did the dos2unix then it worked perfectly fine. The information is also provided in the link provided by Ian. I really appreciate your help. – Saugat Dahal May 27 '20 at 19:41