DronaBlog

Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

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





Monday, December 17, 2018

Unix Interview Questions and Answers - Part I


Are you preparing for Unix interview? Unix is major operating systems on which enterprise applications run. Because of it, the interviewer asks Unix related questions during interviews of technologies such as Java, Hadoop, and Python etc. Are you planning to give an interview for support project for which Unix knowledge is required? If yes, then refer to this article as it provides detailed questions and answers about Unix.

Q 1: What is the process in Unix and What are the types of process exists in Unix?

Answer
A process is an instance of a program running in an Operating System. Normally, the process is started when a program is initiated.

Type of Processes: There are two types of the process
a) Foreground Processes
b) Background Processes


Q 2: What is the command to list the directories and the files?

Answer:
This is a very basic question asked during an interview to understand whether the candidate has basic knowledge of Unix.

The command below is used to list the directories and the files in current location -
$ ls -ltr

ls : To list files
ls -l : To list files with additional information

Every file line begins with d, -, or l. These characters indicate the type of the file which is shown on the screen as result.

e.g
drwrrwxr-x 3 abc abc 16   Jan 10 2018 temp_dir
-rw-rw-r-- 2 abc abc 4028 Mar 5  17:12 test.sh

Learn more: Click here to learn more about ls command.

Q 3: What are the differences between the Zombie and Orphan processes?

Answer: The differences between the Zombie and the Orphan processes are listed as below -

Zombie Process
Orphan Process
Zombie processes are those processes which are killed or completed execution but still show an entry in the process table.
A child process which remains running itself even after its parent process is completed or terminated is called as an orphan process.
Zombies only occupy space in the process table
Orphan Process uses memory resources
If the Zombies that exist for more time, then it indicates an issue in the parent program
The orphan process is get created unknowingly and unintentionally due to process crash
The zombie process shows the process with a Z state
The orphan processes are terminated


ReadClick here, to know more details about the Zombie, Orphan and the Daemon processes.


Q 4: How to run the process in background and foreground?

Answer: Follow the below steps to run a process in
A. Background:
To run any process in the background, we need to '&' sign at the end of the command. By using '&' sign we are telling Unix system that runs the given process in the background so that we can continue to use the command prompt. 

The example of running the process as the background process is as -

sh run_calculate_interest.sh &

B. Foreground:
If any process is running background and you would like to bring it to foreground then use the steps below -
a)  Get job id of the process
$ jobs

The output will look like as 
[1]   7095 Running                 run_calculate_interest&
[2]   7206 Running                 run_app &

b) Use jobid and run it in the foreground as
$ fg 7095

Learn more: Unix tutorial 




Q 5: Assume that one of the processes is running more than 24 hrs. How you are going to identify such process and remove or kill it?

Answer: Sometimes, interviewer tries to create the complex scenario for the simple answer. So do not get confused with the description of the question. The underline principle will remain the same to kill the process. By asking this question, an interviewer is trying to check whether how extensive you worked on Unix system.

To identify long running processes we can use commands such as 'Top' or 'jobs'. However, you can also us 'ps -ef'  command to know running process.

The above command will provide the process ids (or Job ids) for running processes.

Use below command to kill the process-
$ kill -9 PID

Here, PID is process id.









Thursday, November 29, 2018

Very Important Unix Commands


Are you looking for an article for Unix commands which are used for daily activities? Are you looking for the Unix commands which are used for manipulating data? Are also willing to know what commands used for networking and compressing the files? If yes, then you can refer this article as it highlights on commands which are used for data manipulation, networking, messaging and compressing the files.

A) Commands used for manipulating data:


Command
Details
awk
Pattern scanning and processing language
cmp
Compares the contents of two files
comm
Compares sorted data
cut
Cuts out selected fields of each line of a file
diff
Differential file comparator
expand
Expands tabs to spaces
join
Joins files on some common field
perl
Data manipulation language
sed
Stream text editor
sort
Sorts file data
split
Splits the file into smaller files
tr
Translates characters
uniq
Reports repeated lines in a file
wc
Counts words, lines, and characters
vi
Opens vi text editor
vim
Opens vim text editor
fmt
Simple text formatting
spell
Checks text for spelling error
ispell
Checks text for spelling error
emacs
GNU project Emacs
ex, edit
Line editor


B)  Commands used for Compressing files:


Command
Details
compress
Compresses files
gunzip
Helps uncompress gzipped files
gzip
GNU alternative compression method
uncompress
Helps uncompress files
unzip
List, test, and extract compressed files in a ZIP archive
zcat
Cat a compressed file
zcmp
Compares compressed files
zdiff
Compares compressed files
zmore
File perusal filter for crt viewing of compressed text




C)  Commands used for Getting information:


Command
Details
apropos
Locates commands by keyword lookup
info
Displays command information pages online
man
Displays manual pages online
whatis
Searches the whatis database for complete words
yelp
GNOME help viewer


D)  Commands used for Network Communication:


Command
Details
ftp
File transfer program
rcp
Remote file copy
rlogin
Remote login to a Unix host
rsh
Remote shell
tftp
Trivial file transfer program
telnet
Makes terminal connection to another host
ssh
Secures shell terminal or command connection
scp
Secures shell remote file copy
sftp
Secures shell file transfer program




E)  Commands used for sending messages between users:


Command
Details
evolution
GUI mail handling tool on Linux
mail
Simple send or read mail program
mesg
Permits or denies messages
parcel
Sends files to another user
pine
Vdu-based mail utility
talk
Talks to another user
write
Writes message to another user




Refer the video below more interesting concepts about Unix technology -


What are differences between multimerge and merge API in Informatica MDM

                Are you interested in knowing what is the use of multimerge and merge APIs? Are you also would like to know the difference b...