Sponsored Content
The Lounge What is on Your Mind? Speculative Shell Feature Brainstorming Post 302507789 by tetsujin on Thursday 24th of March 2011 08:01:09 PM
Old 03-24-2011
You say I may have a lot to learn - I acknowledge the possibility. If I did not have the conviction to test my ideas like this, I would miss an opportunity to find out where my knowledge is lacking.

I've made a lot of progress along these lines already. Things I thought the shell couldn't do, things I thought it wasn't appropriate for - as I learned more about the advanced features of the shell and how they're used in practice I started to understand some of my understanding was wrong.

This is very important from my perspective. If I don't know what the shell currently provides, I can't hope to successfully "improve" upon it. If I change the shell syntax, and lose various features in the process, I have to understand what those features are and how serious the loss is.

Bash's diverse variable substitution syntax is a good example. It tends to strike me as a giant mess. But it's powerful stuff, too. Things like "This variable, unless it's unset in which case this string instead" or "this variable, but trim a bit of text out of it first" - it's good stuff.

Quote:
Originally Posted by Corona688
Now imagine it had three mutually exclusive ways of handling things -- one for stdin and stdout, one for all other files, and one for strings. By building those things as independent features you've inadvertently built walls between them which programmers will be hitting like grasshoppers on your windshield unless you kludge bridges over them somehow.
You do make a good point here, I think... It may be a good argument against the idea that things I've described can coexist with the more standard shell approach.

Quote:
In a strongly typed language, that's easy. It doesn't have to decide on the fly what routines to use, and can even warn you if you mix and match them in problematic ways.

In the shell it's still doable because they can all be stored as strings. With some work you can still keep it orthogonal, make it do the right thing under any circumstances -- so there's not a conflict.
If everything's stored as a string, or if you pretend it is, then you can't specialize for different data types. For polymorphism to work you need a clear idea of the "class" that defines the object's behavior. If one sticks to the idea that everything should have an automatic translation back to string, then any time you do an operation on that thing there's the question "did you mean this to be a string operation?"

For instance, a fairly common expectation is that string concatenation (which, of course, is not addition) should use the addition operator. In Perl this wouldn't work, because that would create an ambiguity. "1" + "2" = "3" and not "12" - so they have a separate operator instead.

You see this a lot in bash as well. "Is that numeric comparison or lexicographical string comparison?" I kind of hate that, honestly. Smilie

Quote:
How about a new special character like @filehandle? Then you won't get files showing up in places you didn't expect. I wouldn't go too overboard like Perl did, but just one shouldn't end the world.
I think there will be other types of "special" variable. I can't give each one its own special character...

Quote:
If you're inventing a totally new language, I wouldn't just extend a shell, I'd rip out a ton of old stuff too.
Pretty much that's what I'm going for, yeah. A totally new shell, but with familiar elements of Bourne-derived shells in the syntax and operation.

Arithmetic is actually a good example IMO. I feel like the shell should be an environment where that kind of operation comes naturally - it should be easier to express in the shell syntax, I think.

Quote:
Almost anything but pipes could be revamped. I know you'll be tempted to turn <file into an anonymous filehandle instead of stdin, but consider how many programs read from stdin and you might want to keep stdin/stdout as easily available as they are already.
I don't think I've described anything that affects stdin/stdout...

Quote:
Another neat operator could be something like @*.txt | command ...to make shell globbing print to stdout, like find doues. Argument limit, what argument limit?
Didn't they get rid of the argument limit? (In Linux, anyway...?) And did it even apply to builtin echo?

Quote:
What is the difference between an ordinary file and a special file? Presently, nothing -- which is extremely nice.
What I've described as "special file handle objects" - I mostly use that phrase to tag places where I'm talking about some representation of an open file that's not how things currently operate in the shell. I have, in a few places, considered the possibility that these objects could coexist with the current mechanism for opening files (as in "exec 3<filename" or "exec {f}<filename" for the current mechanism, something else for this "special" crap) but if I'm not going for Bourne compatibility I think there's no point.

It's the difference between having "open this file on this specific descriptor table index, leave it open and pass it on to all child processes until I tell you otherwise" and "open this thing, I don't care where, close it when I'm not using it any more and pass it on only when I say so". I think the latter makes a lot more sense.

Quote:
Arrays translate very nicely and usefully into strings.

Code:
$ A[0]=1
$ A[1]=2
$ A[2]=3
$ IFS=","
$ echo "${A[*]}"
1,2,3

Yeeg, there's an awkward distinction...
Code:
$ echo ${A[*]}
1 2 3
$ echo "${A[*]}"
1,2,3
$ x="${A[*]}"
$ echo $x
1 2 3
$ echo "$x"
1,2,3

That is just screwy. (Bash 4 - I don't know what you're using...)

I mean, I get the distinction in the first two commands, even appreciate it: without quotes, ${A[*]} expands each array element to a positional argument, preserving the distinction between neighboring elements - while if you quote it, it's applying $IFS. But what about $x? I set it equal to the quoted version of the contents of A but it preserved that delimitation, as if $x were an array and as if I hadn't included those quotes...

(Obviously when I find things that I think are wrong with bash, that doesn't undermine the merits of bash's basic approach - I don't mean it that way... Just... this seems very inconsistent!)

Consider this:

Code:
# $IFS is still ","
$ A[1]="4,5"
$ echo "${A[*]}"
1,4,5,3

You have lost information in the translation... Hence, I think it's inappropriate to treat arrays as string-equivalent.

Quote:
Quote:
I mean, it's very common to redirect to one of the commonly used file descriptors.
"It's very common to print to the screen, or maybe a file. But who'd ever want to print to a string?" That line of thought left a huge hole in the BASIC language that had to be kludged around.
I'm not saying redirection to other file descriptors shouldn't be provided:
In fact, my personal feeling is quite the opposite. I believe this feature is essential.

However, it's not the common case that someone will need to open a file on a specific FD and have all child processes inherit that file on that specific numeric FD. As such, I think it's reasonable to fit the syntax to what I believe is the more common case of opening a file: within the shell, you don't care what numeric FD it winds up on. It's only when you launch a child process that uses that numeric FD - and most child processes won't.

Quote:
Don't forget the read builtin. Also, the linux flock utility.
What of them?

Quote:
Also don't forget how any program taking a filename can get /proc/self/fd/23 shoehorned into it.
I actually used this in several examples already... (Well, /dev/fd/ - but it's the same thing)

Quote:
Getting rid of that feature wouldn't be a problem for most of my shell scripts. But it would be impossible to write a few small but really important ones.
Let's be clear here: what feature?

Getting rid of numeric redirection?
Code:
cmd 4< file

No. This is valuable.

Getting rid of the traditional mechanism for opening files in the shell, in which the user explicitly selects a numeric FD?
Code:
exec 4<file
cmd --do-something-with-fd=4

Maybe. I know of two programs that operate like this. As such I think this kind of blanket redirection doesn't make a lot of sense. At the very least it shouldn't be the mechanism for opening files in the shell.

Quote:
Quote:
So you look at how rare it is to redirect a file descriptor other than #0 or #1
? You do it all the time.

When you do command < filename do you think the shell opens filename as FD 1? No, it becomes some random FD, and the shell just duplicates it over stdin.
Give me a little credit here. I know how open(), dup(), etc. work. I've read APUE. Best $70 I ever spent. And yes, I actually do need to read it again. Smilie (The first couple times through I had trouble understanding controlling terminals and job control... I think I got it eventually but I'm getting rusty on the details)

But the relevant point is that when the user runs cmd < filename they don't care what that call to open() returned. They don't need to. They never need to provide or obtain the answer to that question. All they care about is that the file is open for reading, "cmd" sees it as FD 0, and the file is closed when the job is done.

Quote:
Quote:
...and then think about how frequently it's useful to pick one of those other file descriptors, open a file on it, and have that file remain open on every job you run until you specify otherwise.
You're beginning to contradict yourself, you just told me almost nothing cares about other open files.
Poor choice of words, perhaps. I was inviting you to think about how frequent that usage is (as in "measure frequency of this occurrence" rather than "boy, is that occurrence frequent!"), with the suggestion that it isn't common. Seriously, do you do that a lot? Say "open this file on decriptor 7" and then actually use descriptor 7 in a bunch of programs, as opposed to redirecting the file to some other FD?

Quote:
...except for special files because they're special.
What are you getting at here? I don't get it. I read it and it just looks like you're being rude, giving me a hard time for using the word "special" or something. I don't know if that's your intent.

Quote:
All this new special syntax is wholly unnecessary. Just make some sort of exec-substitute that returns a file-descriptor number in the form of a string gets you everything you wanted and more, because leaving it as general-purpose lets you use it in ways you didn't originally think of.
It doesn't get me scoped file handles, or control over which child processes inherit that open file descriptor, which is kind of where this whole thing started. Smilie

As for fileno():
Within the shell it wouldn't normally be important where an open file resides. The use case would be to dup the file descriptor to some other number via redirection syntax:
Code:
# $fh is some "file handle" - a managed open file descriptor.
# User doesn't generally care what its number is, and its behavior in substitutions is not entirely straightforward.
$ read <& $fh
# Dup it to FD 0 to use it with read
$ some-proc --using-fd=7 7<& $fh
# Need it on another FD?  No problem...

Mostly there's no point in knowing where it is as long as you can control where it's going to be.

But there's no reason a feature couldn't be included to determine where the file is. There's not a lot of value to that (debugging the shell would be one possibility, I guess?) but it's doable.
 

4 More Discussions You Might Find Interesting

1. SCO

BASH-like feature. is possible ?

Greetings... i was wondering if there is any shell configuration or third party application that enables the command history by pressing keyboard up arrow, like GNU/BASH does or are there an SCO compatible bash versio to download? where ? just wondering (sory my stinky english) (2 Replies)
Discussion started by: nEuRoMaNcEr
2 Replies

2. Shell Programming and Scripting

Creating a command history feature in a simple UNIX shell using C

I'm trying to write a history feature to a very simple UNIX shell that will list the last 10 commands used when control-c is pressed. A user can then run a previous command by typing r x, where x is the first letter of the command. I'm having quite a bit of trouble figuring out what I need to do, I... (2 Replies)
Discussion started by: -=Cn=-
2 Replies

3. UNIX for Dummies Questions & Answers

brainstorming automated response

I am managing a database of files for which there is a drop-box and multiple users. what i would like to do is set a criteria for files coming into the drop-box based on file structure. (they must be like this W*/image/W*-1234/0-999.tif) If the files do not match the criteria i want them to be... (1 Reply)
Discussion started by: Movomito
1 Replies

4. UNIX for Beginners Questions & Answers

Can we create any check-point feature in shell ?

I have a script as below and say its failed @ function fs_ck {} then it should exit and next time i execute it it should start from fs_ck {} only Please advise #!/bin/bash logging {} fs_ck {} bkp {} dply {} ## main function### echo Sstarting script echo '####' logging fs_ck... (3 Replies)
Discussion started by: abhaydas
3 Replies
All times are GMT -4. The time now is 05:20 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy