[SOLVED] Please explain how this Read Command works
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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>.
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?
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.
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.
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?
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
[]
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.