LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-12-2018, 08:01 AM   #1
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Rep: Reputation: 19
Question bash redirection & other


My GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) manual and the bash itself puzzle me on a number of issues.


REDIRECTION

Code:
$ : 7<test.txt # does nothing, it should have created /dev/fd/7 
$ ll /dev/fd/
total 0
lrwx------. 1 user user 64 Jan 12 15:16 0 -> /dev/pts/2
lrwx------. 1 user user 64 Jan 12 15:16 1 -> /dev/pts/2
lrwx------. 1 user user 64 Jan 12 15:16 2 -> /dev/pts/2
lr-x------. 1 user user 64 Jan 12 15:16 3 -> /proc/3401/fd
$ : {my}<test.txt # works, this was supposed to be a special case of the above 
$ ll /dev/fd/
total 0
lrwx------. 1 user user 64 Jan 12 15:16 0 -> /dev/pts/2
lrwx------. 1 user user 64 Jan 12 15:16 1 -> /dev/pts/2
lr-x------. 1 user user 64 Jan 12 15:16 10 -> /home/user/tmp/test.txt
lrwx------. 1 user user 64 Jan 12 15:16 2 -> /dev/pts/2
lr-x------. 1 user user 64 Jan 12 15:16 3 -> /proc/3402/fd
$ : 10<&- # does nothing, it was supposed to delete /dev/fd/10
$ ll /dev/fd/
total 0
lrwx------. 1 user user 64 Jan 12 15:17 0 -> /dev/pts/2
lrwx------. 1 user user 64 Jan 12 15:17 1 -> /dev/pts/2
lr-x------. 1 user user 64 Jan 12 15:17 10 -> /home/user/tmp/test.txt
lrwx------. 1 user user 64 Jan 12 15:17 2 -> /dev/pts/2
lr-x------. 1 user user 64 Jan 12 15:17 3 -> /proc/3407/fd
$ : 11<&10- # deletes 10 as long as 11 does not exist, it was supposed to move 10 in 11 
$ ll /dev/fd/
total 0
lrwx------. 1 user user 64 Jan 12 15:17 0 -> /dev/pts/2
lrwx------. 1 user user 64 Jan 12 15:17 1 -> /dev/pts/2
lrwx------. 1 user user 64 Jan 12 15:17 2 -> /dev/pts/2
lr-x------. 1 user user 64 Jan 12 15:17 3 -> /proc/3408/fd

Simple Commands

Code:
$ a=1 echo $a # returns an empty line instead of '1'

Arrays

"Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid conflicts
with pathname expansion. "
Braces are required to include [subscript] in name. Conflicts with pathname expansion do not appear:
Code:
$ echo $my[1]
[1]
$ touch my1
$ echo $my[1]
[1]

Shell Variables

How can I verify the content of IFS? I'd like to see it.
 
Old 01-12-2018, 08:17 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
what do you really want now? are these statements, or issues, do you want to ask something or ???
see http://www.tldp.org/LDP/abs/html/arrays.html about arrays (do not forget about { } )!
about redirection:
http://www.tldp.org/LDP/abs/html/io-redirection.html
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html
about IFS probably:
echo "${IFS}" | od -xc

Last edited by pan64; 01-12-2018 at 08:19 AM.
 
1 members found this post helpful.
Old 01-12-2018, 10:30 AM   #3
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Original Poster
Rep: Reputation: 19
Why does it not work as it was supposed to? Do I misread the manual? Is the manual inconsistent with the actual program?
How do I perform those tasks? How do I create a redirect to a file descriptor I choose, not to a file descriptor chosen by bash, bigger or equal to 10?
How do I close a file descriptor without using some number of a file descriptor which does not exist?
How do I duplicate a file descriptor in this version of bash?

I understand that bash can not be modified for backward compatibility reasons, but the manual certainly can be modified.
 
Old 01-12-2018, 10:36 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
which documentation/manual do you use?
 
Old 01-12-2018, 10:37 AM   #5
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Original Poster
Rep: Reputation: 19
Your links lead to much clearer explanations than my bash manual, but still they do not clarify my questions. Maybe the features I can not find have just been removed from my bash?! That would be very bad for portability. And n<&- still does not work, as I show above. Does it work on your system? Is my example correct? I mean, was it supposed to work as expected? Maybe the file descriptor is closed but I still see it? No, it still works.

Last edited by doru; 01-12-2018 at 10:46 AM.
 
Old 01-12-2018, 10:39 AM   #6
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Original Poster
Rep: Reputation: 19
Bash is Copyright (C) 1989-2011 by the Free Software Foundation, Inc.
GNU Bash-4.2 2010 December 28

It came with bash on Centos 7.

Thank you for your help.

Please try those commands on your version.

Last edited by doru; 01-12-2018 at 10:51 AM.
 
Old 01-12-2018, 11:16 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Regarding the /dev/fd/, this is highly dependend of the kernel version what happens there.
What matters is that
Code:
: 7<test.txt
has really temporary associated a descriptor 7 with the file test.txt.
That this is implemented by /dev/fd/7 is an assumption (mostly true), and that /dev/fd/7 remains is another assumption.

Code:
a=1 echo $a
does not work because $a is evaluated too early.
Also this is a temporary assignment.
The following works
Code:
a=1 sh
$ echo $a
$ exit
Or
Code:
a=1 sh -c 'echo $a'
Another demonstration
Code:
a=2 # the local shell variable
a=1 sh -c 'echo $a'
1
a=1 sh -c "echo $a"
2
Even within " " the shell substitutes $a with 2, then forks, the forked shell sets a=1 in its environment, then execs to sh -c ... and the sh prints the 2.
But in ' ' the shell does not substitute $a so the sh takes it from its environment.
Array handling denotes braces.
Code:
echo ${my[1]}
is the second index (starts with 0).
A demonstration
Code:
my=( first second )
echo "${my[1]} ${my[0]}"
echo "my has ${#my} elements, that are: ${my[@]}"

Last edited by MadeInGermany; 01-12-2018 at 11:21 AM.
 
1 members found this post helpful.
Old 01-12-2018, 11:21 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
if I understand well something like 7<test.txt is not specified at all, no such thing documented. That's why I asked where is it from?
 
Old 01-12-2018, 11:29 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
For viewing IFS I would redirect it to a file and view it with vi
Code:
$ echo "$IFS" > IFS
$ vi IFS
:set list
The standard IFS consists of
  • a space character
  • a tab character
  • a newline character
 
1 members found this post helpful.
Old 01-12-2018, 12:53 PM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by pan64 View Post
if I understand well something like 7<test.txt is not specified at all, no such thing documented. That's why I asked where is it from?
It runs a command with file descriptor 7 redirected from the named file. In the case of the ":" (no-op) command, or any other command that is not going to read from FD 7, it is pointless. If you want that redirection to be available for the rest of the current script, you use the "exec" command:
Code:
exec 7<test.txt
Quote from the bash manpage:
"Redirecting Input
Redirection of input causes the file whose name results from the expansion of word to be opened for reading on file descriptor n, or the standard input (file descriptor 0) if n is not specified.

The general format for redirecting input is:
[n]<word
"
 
2 members found this post helpful.
Old 01-12-2018, 02:14 PM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
so the documentation I read was too old.
 
Old 01-13-2018, 09:13 AM   #12
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by MadeInGermany View Post
Regarding the /dev/fd/, this is highly dependend of the kernel version what happens there.
What matters is that
Code:
: 7<test.txt
has really temporary associated a descriptor 7 with the file test.txt.
That this is implemented by /dev/fd/7 is an assumption (mostly true), and that /dev/fd/7 remains is another assumption.
Thank you very much, this is wonderful:
Code:
$ { ll /dev/fd/; ll /dev/fd/ 7<&-; ll /dev/fd/; } 7<test.txt
total 0
lrwx------. 1 safe safe 64 Jan 13 17:05 0 -> /dev/pts/1
lrwx------. 1 safe safe 64 Jan 13 17:05 1 -> /dev/pts/1
lrwx------. 1 safe safe 64 Jan 13 17:05 2 -> /dev/pts/1
lr-x------. 1 safe safe 64 Jan 13 17:05 3 -> /proc/4000/fd
lr-x------. 1 safe safe 64 Jan 13 17:05 7 -> /home/safe/tmp/test.txt
total 0
lrwx------. 1 safe safe 64 Jan 13 17:05 0 -> /dev/pts/1
lrwx------. 1 safe safe 64 Jan 13 17:05 1 -> /dev/pts/1
lrwx------. 1 safe safe 64 Jan 13 17:05 2 -> /dev/pts/1
lr-x------. 1 safe safe 64 Jan 13 17:05 3 -> /proc/4001/fd
total 0
lrwx------. 1 safe safe 64 Jan 13 17:05 0 -> /dev/pts/1
lrwx------. 1 safe safe 64 Jan 13 17:05 1 -> /dev/pts/1
lrwx------. 1 safe safe 64 Jan 13 17:05 2 -> /dev/pts/1
lr-x------. 1 safe safe 64 Jan 13 17:05 3 -> /proc/4002/fd
lr-x------. 1 safe safe 64 Jan 13 17:05 7 -> /home/safe/tmp/test.txt
All your answers are great!
 
Old 01-13-2018, 09:28 AM   #13
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by MadeInGermany View Post
Code:
echo ${my[1]}
is the second index (starts with 0).
A demonstration
Code:
my=( first second )
echo "${my[1]} ${my[0]}"
echo "my has ${#my} elements, that are: ${my[@]}"
${#my} returns the number of characters in my[0], ${#my[*]} returns the number of elements in my.

Last edited by doru; 01-13-2018 at 10:42 AM.
 
1 members found this post helpful.
Old 01-15-2018, 08:06 AM   #14
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Thanks for correcting my mistake.
Indeed the #elements is ${#my[*]} or ${#my[@]}.
 
  


Reply

Tags
bash, manual, redirection



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
bash redirection help clifford227 Programming 6 07-30-2012 02:36 PM
LXer: Qemu-kvm 0.15.1 & Spice(0.10.0) & Libvirt 0.9.7-2 USB Redirection support for Ubuntu Precise a LXer Syndicated Linux News 0 11-14-2011 11:30 AM
LXer: Set up Qemu&Spice&Libvirt 0.9.6 USB Redirection on Ubuntu 11.10 LXer Syndicated Linux News 0 10-16-2011 02:40 PM
Bash Redirection question jcllings General 5 08-26-2009 10:10 AM
BASH scripting: confused about redirection & file descriptors funkymunky Programming 1 06-07-2004 07:47 AM

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

All times are GMT -5. The time now is 04:38 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
Open Source Consulting | Domain Registration