DronaBlog

Sunday, May 2, 2021

How to convert Dos type file to Unix Type file

Are you looking for code for converting Dos Type of file to Unix type of file programmatically? Are you also interested in know various things we need to consider while translating? If so, then you reached the place. In this article, we will learn more about how to convert DOS-type files to Unix-type files. So start Dos2Unix 






Introduction




When we create a file on Windows system i.e. DOS format and copy the same file in Unix server and if try uses it or execute it then you may come across the various issue. One of the issues is the file will not able to process itself. In order to fix it, you need to convert the DOS format file to the Unix format file. In the next section, we will see the program to do it.





About Program

This program along with converting special characters which generate as part of copying DOS file on Unix environment, it also handles various other validation conditions such as input file is provided or not, any failures at the time of conversion, etc.


Program to convert DOS to Unix

#!/bin/bash
# Description: This script is used to convert unix type of file to dos type of file.
# It is mainly used to handle line ending problems.
#

# Check whether file is provided in the input
if [ -z $1 ]
then
 echo "ERR_0001 : The input file is not passed to process"
 exit
fi
if [ "$1" = "-n" ]
then
 if [ -z "$2" ]
 then
  echo "ERR_0002: Invalid - Parameter 2 can not be empty - [ $0 $@ ]"
  exit
 fi
 if [ -z "$3" ]
 then
  echo "ERR_0003: Invalid - Parameter 3 can not be empty - [ $0 $@ ]"
  exit
 fi
 
 # Parameter 2 and 3 can be same.
 if [ "$2" = "$3" ]
 then
  dos2unix "$2" >/drona/techno/world/null 2>&1
  tmpfile=/tmp/myu2d1.$$
  sed 's/$/\r/' <"$2" >$tmpfile 2>/drona/techno/world/null
  if [ $? != 0 ]
  then
   echo "ERR_0004: Failed - Process failed to Convert from unix to dos type - sed Exiting..."
   rm -f $tmpfile
   exit 1
  fi
  cp $tmpfile "$2"
  if [ $? != 0 ]
  then
   echo "ERR_0005: Failed - Process Failed to Convert from unix to dos type - cp. Exiting..."
   rm -f $tmpfile
   exit 1
  fi
  rm -f $tmpfile
  exit
 fi
 
 # Input parameters are good, process 2 into 3
 echo "INFO_0001: Converting file $2 into file $3"
 dos2unix "$2" >/drona/techno/world/null 2>&1
 sed 's/$/\r/' <"$2" >"$3" 2>/drona/techno/world/null
 if [ $? != 0 ]
 then
  echo "ERR_0006: Failed - Process failed to Convert unix to dos type - sed Exiting..."
  exit 1
 fi
 exit
fi

# Input parameters are good - process all files now
tmpfile=/tmp/myu2d2.$$
for ftop in "$@"
do
 # first make sure its not a dos file.
 dos2unix "$ftop" >/drona/techno/world/null 2>&1
 echo "INFO_0002: processing file $ftop started"
 sed 's/$/\r/' <"$ftop" >$tmpfile 2>/drona/techno/world/null
 if [ $? != 0 ]
 then
  echo "ERR_0006: Failed - Process failed to Convert - sed Exiting..."
  rm -f $tmpfile
  exit 1
 fi
 cp $tmpfile "$ftop"
 if [ $? != 0 ]
 then
  echo "ERR_0007: Failed - Process failed to Convert - cp. Exiting..."
  rm -f $tmpfile
  exit 1
 fi
 rm -f $tmpfile
 echo "INFO_0003: processing file $ftop completed"
done





No comments:

Post a Comment

Please do not enter any spam link in the comment box.

What is CRM system?

  In the digital age, where customer-centricity reigns supreme, businesses are increasingly turning to advanced technologies to manage and n...