Re: OT: bash script - unexpected exit

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Andre Speelmans napsal(a):
>> You shouldn't rely on cat or for to read a file line by line, but instead do
>> this:
>>
>> while read line; do
>>   commands
>> done < hosts
> 
> I would pick this form myself, but why should one not rely on cat? It
> seems to me a viable (even if useless use of cat) option.
> Is there something totally wrong with relying on cat to read a file?

main difference is probably in fact that commands in pipe are executed
in subshell - and it means 1) some extra processes, and 2) subshell
environment variables are not passed to parent shell.

Thus script:
#!/bin/bash
echo "1: BASH_SUBSHELL=$BASH_SUBSHELL, PPID=$PPID, BASHPID=$BASHPID, \$\$=$$."
N=0
cat /etc/adjtime | while read line; do
  ((N++))
  echo "  2: BASH_SUBSHELL=$BASH_SUBSHELL, PPID=$PPID, BASHPID=$BASHPID, \$\$=$$, N=$N, line:$line."
done
echo "3: N=$N."

will show:
1: BASH_SUBSHELL=0, PPID=5671, BASHPID=7900, $$=7900.
  2: BASH_SUBSHELL=1, PPID=5671, BASHPID=7902, $$=7900, N=1, line:-0.584209 1326733538 0.000000.
  2: BASH_SUBSHELL=1, PPID=5671, BASHPID=7902, $$=7900, N=2, line:1326733538.
  2: BASH_SUBSHELL=1, PPID=5671, BASHPID=7902, $$=7900, N=3, line:UTC.
3: N=0.

And script:
#!/bin/bash
echo "1: BASH_SUBSHELL=$BASH_SUBSHELL, PPID=$PPID, BASHPID=$BASHPID, \$\$=$$."
N=0
while read line; do
  ((N++))
  echo "  2: BASH_SUBSHELL=$BASH_SUBSHELL, PPID=$PPID, BASHPID=$BASHPID, \$\$=$$, N=$N, line:$line."
done </etc/adjtime
echo "3: N=$N."

will show:
1: BASH_SUBSHELL=0, PPID=5671, BASHPID=7923, $$=7923.
  2: BASH_SUBSHELL=0, PPID=5671, BASHPID=7923, $$=7923, N=1, line:-0.584209 1326733538 0.000000.
  2: BASH_SUBSHELL=0, PPID=5671, BASHPID=7923, $$=7923, N=2, line:1326733538.
  2: BASH_SUBSHELL=0, PPID=5671, BASHPID=7923, $$=7923, N=3, line:UTC.
3: N=3.
-- 
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org


[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux