LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-27-2015, 02:29 AM   #1
murali2489
LQ Newbie
 
Registered: Jan 2015
Posts: 14

Rep: Reputation: Disabled
Please explain how this Read Command works


Hi All,

I am new to linux and I am finding difficulties in understanding the below commands.

Please explain what is happening in the below commands.

Code:
IFS="~"
exec 4< $BCP_DIR/cnts.data
while read -ru4 TableName Count CreationDateTime Type AccessDate
do
        . /apps/rmb/prod/scripts/load_datasum.ksh
done
IFS=" "
exec 4<&-

Please explain how this commands flows especially
exec 4< $BCP_DIR/cnts.data & exec 4<&-
 
Old 01-27-2015, 04:07 AM   #2
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,682

Rep: Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567
Please read the REDIRECTION section of 'man bash'.
 
Old 01-27-2015, 09:44 AM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,618

Rep: Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577
In fairness, there is some rather uncommon stuff in that script. Let's break it down:
Code:
IFS="~"
Change the internal field separator to "~". Presumably, the file to be read is using that character as a field separator.
Code:
exec 4< $BCP_DIR/cnts.data
Continue execution with the named file open for reading on (the otherwise unused) file descriptor 4.
Code:
while read -ru4 TableName Count CreationDateTime Type AccessDate
Execute a loop as long as the read command returns data. The read command will read from file descriptor 4 ("-u4" option), will treat backslash as an ordinary character ("-r" option), and will store the first 4 fields into variables TableName, Count, CreationDateTime, and Type, and the remainder of the line in variable AccessDate.
Code:
        . /apps/rmb/prod/scripts/load_datasum.ksh
Read and execute the commands from that file in the current shell.
Code:
IFS=" "
Restore the internal field separator. Something's not quite right there. IFS would normally contain the three characters <space>, <tab>, and <newline>.
Code:
exec 4<&-
Close file descriptor 4.

Last edited by rknichols; 01-27-2015 at 09:47 AM.
 
1 members found this post helpful.
Old 01-27-2015, 02:14 PM   #4
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 7
Posts: 693

Rep: Reputation: 69
Quote:
Originally Posted by allend View Post
Please read the REDIRECTION section of 'man bash'.
This is a very beautiful answer. Not atypical, though, but really beautiful. You might as well have pointed the middle finger, stating it more clearly that you won't make any attempt to help him in any way.


Normally, in order to restore IFS, another variable that takes the value of IFS should be declared.
Code:
oIFS=$IFS
$IFS="~"
... ... ...
$IFS=oIFS
In this way, you make sure that the value of IFS remains the same, right?

Last edited by vincix; 01-27-2015 at 02:20 PM.
 
1 members found this post helpful.
Old 01-27-2015, 05:35 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,618

Rep: Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577
Quote:
Originally Posted by vincix View Post
Code:
oIFS=$IFS
$IFS="~"
... ... ...
$IFS=oIFS
In this way, you make sure that the value of IFS remains the same, right?
Correct, and that's what I always do. In the current case, though, we don't know what follows in that script. Perhaps the setting to just <space> was intentional. Or, maybe that script has gone through enough copying/pasting from a screen display that the other characters have just been lost.

And of course if the script ends at that point, there wasn't really any need to restore IFS at all.
 
1 members found this post helpful.
Old 01-27-2015, 07:15 PM   #6
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,682

Rep: Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567
Quote:
You might as well have pointed the middle finger, stating it more clearly that you won't make any attempt to help him in any way.
My post #2 was addressed at the specifics of
Quote:
Please explain how this commands flows especially
exec 4< $BCP_DIR/cnts.data & exec 4<&-
There was insufficient detail in the initial post to know whether the code given comes from a real world situation or a contrived teaching exercise. You are not encouraged to do other people's homework at LQ.
 
Old 01-27-2015, 08:02 PM   #7
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 7
Posts: 693

Rep: Reputation: 69
Quote:
Originally Posted by allend View Post
My post #2 was addressed at the specifics of

There was insufficient detail in the initial post to know whether the code given comes from a real world situation or a contrived teaching exercise. You are not encouraged to do other people's homework at LQ.
I agree, but doing someone's homework is the exact opposite of what you've written. There's room in-between, just saying... Not that it matters too much, as a lot of users here simply dismiss OPs with 'read the manual' without deigning to give the slightest explanation. Why would this forum even be used if users wouldn't want proper human explanations related exactly to their problems?

Last edited by vincix; 01-27-2015 at 08:04 PM.
 
1 members found this post helpful.
Old 01-28-2015, 03:59 AM   #8
murali2489
LQ Newbie
 
Registered: Jan 2015
Posts: 14

Original Poster
Rep: Reputation: Disabled
Hi rknichols,

Thanks Very Much for the detailed explanation.

I now understood it clearly

Hi vincix,

Thanks for the suggestion ....
Quote:
Originally Posted by allend View Post
My post #2 was addressed at the specifics of

There was insufficient detail in the initial post to know whether the code given comes from a real world situation or a contrived teaching exercise. You are not encouraged to do other people's homework at LQ.

This is a real world scenario, I just posted where im getting doubt.

The functioning of the script is to weekly load the bulk data coming from Downstream to MSSQL Database tables using this script. The files mentioned in the post contains the information of the counts of data being inserted.


I am new to Linux field, that's why Im facing difficulties in Understanding the commands.

Now I see this command in the same script .

Please explain what this below command does and also advise any book or online tutorial for learning Shell commands.

Code:
if [[ -f $BCP_DIR/clmcldt.data ]]
then
   print -n "loading ClosedDate " ;date
    ed - $BCP_DIR/clmcldt.data <<-[] >$LOG_DIR/load_clmcldt.edit.log
        H
        g/ /s///gp
        w
        q
[]
 
Old 01-29-2015, 06:11 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Try reading these, and more importantly actually start writing small scripts; there's no substitute for experience - guides are just that.

http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

there are a million bash guides on the net ....

The only exotic bit of that script is calling the 'ed' editor as a cmd instead of interactively - an odd way to do it ...
 
Old 01-29-2015, 06:58 AM   #10
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,682

Rep: Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567
Actually the script is written for the Korn shell (ksh). Note the sourced file in post #1 (load_datasum.ksh) and the use of print in post #8.

The code is testing for the presence of a file $BCP_DIR/clmcldt.data. If the file exists, the ed editor is being used to remove all spaces in the file. The edits are being logged to the file $LOG_DIR/load_clmcldt.edit.log
The actual ed commands are:
H - Turn on descriptive errors
g/ /s///gp - Globally search for lines containing a space and replace with nothing for every occurrence of space in the line, printing the line when substitutions occur
w - write the resulting file
q - quit
 
1 members found this post helpful.
Old 01-30-2015, 02:00 AM   #11
murali2489
LQ Newbie
 
Registered: Jan 2015
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thanks chrism01 and Allen for explanation ... !!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Could somebody explain how this line of code works... trist007 Programming 2 05-12-2009 02:50 PM
Can someone explain how this script works? Sanborn Programming 18 06-15-2007 09:53 AM
Please explain the way the program works. Gins Programming 7 03-22-2006 07:17 PM
Please explain how dd works and what it does ohovus Linux - Newbie 4 12-13-2005 09:48 AM
Please explain to me how VDR works with DVB? jimdaworm Linux - Software 2 09-04-2004 05:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration