Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Two Scripts and One Scripting Tip

DZone's Guide to

Two Scripts and One Scripting Tip

The way that the reusable modules, functions, and methods were written in this code is very interesting. See why and get a helpful tip for using echo commands in Bash.

· Big Data Zone
Free Resource

NoSQL & Big Data Integration through standard drivers (ODBC, JDBC, ADO.NET). Free Download

Recently, I had to develop a few scripts as part of the data pipeline that we were implementing for a big data project. One script was developed in Bash, while the other script was developed in Windows (DOS) Batch. In each of the scripts, we had to write messages to a log file and also prefix each message with a timestamp.

For this post, the scripts are not important. What is interesting is the way that a reusable module/function/method was written in each of the scripts.

Script Snippet for Bash

This is a snippet for a reusable module and the way it can be invoked, written in Bash:

writeToGlobalLog() {
    # this command writes the first argument to the log file
    echo "`date`: $1" >> $GLOBAL_LOG_FILE
# this command writes all arguments to the log file
#echo "`date`: $*" >> $GLOBAL_LOG_FILE
}

# the mechanism to invoke the script
writeToGlobalLog "Application started"

Script Snippet for DOS Batch

Writing a reusable snippet in DOS batch is a bit complex, as shown below:

@echo off
rem invoke the reusable method
call :writetologfile Application started
pause

rem create timestamp
:timestamp
set SAVESTAMP=%DATE:/=-%@%TIME::=-%
set SAVESTAMP=%SAVESTAMP%%
exit /b 0

rem write all arguments to log file
:writetologfile
call :timestamp
rem echo "%SAVESTAMP%: %*" >> %PATH_TO_LOG_FILE%
echo "%SAVESTAMP%: %*"
exit /b 0

Tip for Bash

If you write a method/function in bash, it can return a value to the caller. This is done using the echo command. But there is a catch: If you use multiple echo commands inside the method — for debugging purposes — these commands interfere with the return value of the function. Hence, only one echo command can be used in the method:

checkGreaterThanFive() {
  # uncommenting the following echo command creates problems with the return value of the method
  #echo $1

  if [[ $1 -gt 5 ]]; then
    retVal="TRUE"
  else
    retVal="FALSE"
  fi

  echo ${retVal}
}

And that's it!

Easily connect any BI, ETL, or Reporting tool to any NoSQL or Big Data database with CData Drivers (ODBC, JDBC, ADO.NET). Download Now

Topics:
bash ,dos ,big data

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}