LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-30-2020, 10:19 PM   #16
centguy
Member
 
Registered: Feb 2008
Posts: 468

Original Poster
Blog Entries: 1

Rep: Reputation: 38

pan64:

I am not good at bash but should be ok for C or Fortran.

Your suggestion works. But one has to read till the end of do loop to realize
that indexfile is to be read. In C or Fortran, we open a file and then our mind
knows what to do to with reference to the open file.

I do not know exact what -r does in read. Sorry but I did a quick man read but there are just too much info.

Let me use what I know until I hit a hard wall again.

Anyway, it is good to keep an open mind. Good discussion!
 
Old 07-01-2020, 01:54 AM   #17
shruggy
Member
 
Registered: Mar 2020
Posts: 950

Rep: Reputation: Disabled
Quote:
Originally Posted by centguy View Post
I do not know exact what -r does in read.
From the Bash Reference Manual:
Quote:
-r
If this option is given, backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not then be used as a line continuation.
From the POSIX standard:
Quote:
-r
Do not treat a <backslash> character in any special way. Consider each <backslash> to be part of the input line.

Last edited by shruggy; 07-01-2020 at 01:57 AM.
 
Old 07-01-2020, 01:58 AM   #18
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,394

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
There is a logical extension that would allow to prepend a redirection to a code block:

Code:
< indexfile while read -r jid
do
    jobname="batch$jid"
....
done
Unfortunately only zsh supports it.

Why logical? Because it's generally supported for a simple command, for ex
Code:
< infile > outfile grep "[0-9]"

Last edited by MadeInGermany; 07-01-2020 at 02:01 AM.
 
Old 07-01-2020, 02:43 AM   #19
centguy
Member
 
Registered: Feb 2008
Posts: 468

Original Poster
Blog Entries: 1

Rep: Reputation: 38
In the pursue of simplicity and knowing exactly what one is doing, this begs a question, why should -r
there in the first place? My indexfile does not have any backslash. There is no need to add burden to the code. May
be I have missed something.

Quote:

[root@centos69-Lenovo t]# cat indexfile
0011
0012
[root@centos69-Lenovo t]# cat a
while read jid
do
jobname="batch$jid"
echo $jobname
done < indexfile
[root@centos69-Lenovo t]# ./a
batch0011
batch0012
The point is without -r, the code still works.
 
Old 07-01-2020, 03:59 AM   #20
shruggy
Member
 
Registered: Mar 2020
Posts: 950

Rep: Reputation: Disabled
The -r is usually added when reading user input from the terminal, to prevent shell script from being tricked out by smart(ass) users. Many of us made it a habit to always specify -r after read, just in case. Better safe than sorry.

Mind you, I don't have this habit, but I find it justifiable. It's part of the "always validate your input" principle.
 
Old 07-01-2020, 06:27 AM   #21
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,653

Rep: Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764
I would suggest you to use shellcheck to analyze your script. It will explain why -r is recommended: https://github.com/koalaman/shellcheck/wiki/SC2162
 
1 members found this post helpful.
Old 07-01-2020, 09:26 AM   #22
centguy
Member
 
Registered: Feb 2008
Posts: 468

Original Poster
Blog Entries: 1

Rep: Reputation: 38
Tried out
But that uses gui.

Any quick way to install and run on CentOS system?
like shellcheck ./myscript

Not urgent though.
 
Old 07-01-2020, 09:29 AM   #23
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,653

Rep: Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764
yes, that is written on that site. Just you need to scroll down a bit (first click on "Read more about")
by the way there should be a package named shellcheck, did you try it?
 
Old 07-01-2020, 10:24 AM   #24
shruggy
Member
 
Registered: Mar 2020
Posts: 950

Rep: Reputation: Disabled
ShellCheck 0.3.8 for CentOS 7 is in the EPEL repository. Rebuilding ShellCheck 0.7.0 from Fedora Rawhide on CentOS 8 is too much hassle though. IIRC, it requires too many ghc-* packages that CentOS 8 doesn't provide. Neither does EPEL-8. Compiling over 15 ghc-* packages is no fun. I did it once on CentOS 7 to build ShellCheck 0.5.0 there. Just use the online checker.

OTOH, ghc-rpm-macros and ghc-rpm-macros-extra are currently in EPEL-8 testing pipeline. So, EPEL is likely to provide additional ghc-* packages in the future.
 
Old 07-01-2020, 10:32 AM   #25
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,653

Rep: Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764Reputation: 4764
you can download [ready-made] docker images, you can have precompiled binaries and also you may try to take a newer version from a newer package, but that may require some additional preparation to make it work.
I would recommend the precompiled binary. https://github.com/koalaman/shellcheck#installing
 
Old 07-01-2020, 09:44 PM   #26
centguy
Member
 
Registered: Feb 2008
Posts: 468

Original Poster
Blog Entries: 1

Rep: Reputation: 38
i am not brave enough to rely on epel.
Will learn docker way to run precompiled binary when I really need to. For now, GUI is fine. Thanks.
 
  


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
[SOLVED] Once again... awk.. awk... awk shivaa Linux - Newbie 13 12-31-2012 04:56 AM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
real, real, real, basic computer for my Mom shengchieh General 3 12-18-2005 12:02 AM
Real Programmers Real People Real CS Students nakkaya General 5 07-04-2003 02:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:24 AM.

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