Sponsored Content
The Lounge What is on Your Mind? Speculative Shell Feature Brainstorming Post 302507770 by Corona688 on Thursday 24th of March 2011 05:52:53 PM
Old 03-24-2011
Maybe there's a better way I can explain myself. I originally started programming in gwbasic.

Code:
LET B=1
PRINT "Okay, B is " + B

Almost immediately I wanted to feed PRINT's output into a string. There's no direct way to do it. I ended up saving its output into a file then reading it back, which worked.

Eventually I graduated to quickbasic, which let me make subroutines. Out of curiosity, I tried to see if I could make my own PRINT.

Turns out, you can't. Print's a builtin, which makes it "special".
  1. You can't make a sub that takes an unknown number of arguments.
  2. Arguments have to be seperated by "," so a sub will never work quite the same way as PRINT.
  3. All subroutines must be called with CALL. You can't just say the name and have it work.

You can't have a variable that does anything either, even though there are some are kludged into in BASIC as builtins -- like DATE$, which sets the date. (and causes havoc for vbscript writers who aren't aware of this behavior.)

Later I tried to make a subroutine that evaluated expressions. Not flexible enough. What I eventually had to do was convert the expression it was fed into a .BAS file, and execute another instance of qbasic to evaluate it. Basic's not orthogonal: You can't use the things you build for it the same way you can use the things that're built in.

Even the worst Bourne shell makes these problems almost trivial, because -- for all its faults -- the Bourne shell is very orthogonal. There's almost no corners where environment vars, global vars, local vars, files, strings, builtins, aliases, functions or external programs have to be used in one particular way over another, and it can feed its own output and expressions back into itself seamlessly and Do The Right Thing(tm).

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.

Quote:
Originally Posted by tetsujin
Why? Programming languages are representations of ideas. As long as the ideas are conceptually sound, why does it matter if the back-end implementation is different? We use the same operators for integer math as we do for floating point math, right?
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.

Files would be a whole new kettle of fish. Suddenly variables are special things that might be strings, might be files, might be syntax errors, might be I/O errors, or might just block. Everything gets more complicated.

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.
Quote:
My system already has five different Bourne-compatible shells installed. If I need a Bourne-compatible shell, I can run one of those.
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.

Which syntax for arithmetic do you prefer?

Code:
# old-fashioned hack
C=`expr $B + 5`
# good old-fashioned BASIC
let A=B+5
# newer C-like
((C=B+5))

etc, etc, etc. There's tons of redundancy that could be stripped out.

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.

Another neat operator could be something like @*.txt | command ...to make shell globbing print to stdout, like find doues. Argument limit, what argument limit?
Quote:
#2 is a big one, because the Bourne shell syntax for opening a file isn't applicable to creating a "special file descriptor" thing.
What is the difference between an ordinary file and a special file? Presently, nothing -- which is extremely nice. Remember you're building a language which people other than you will use. If you don't allow possibilities you didn't think of, users can't program in ways you didn't think of.
Quote:
Perl is messy because it was designed to be messy: a quick and dirty means of solving problems, using syntax people would be familiar with from other tools. As such, it's kind of ugly but generally regarded as being quite effective. IMO the shell is seriously limited by this "everything is a string" philosophy. The shell should be a powerful tool for tying other tools together - but instead it's barely adequate for the job.
I think you just have a lot more to learn about it.
Quote:
Shells already do support variable types that "don't translate nicely to strings". They do so because it's a useful feature. Arrays in general, associative arrays in particular.
Arrays translate very nicely and usefully into strings.

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

...which you can also use backwards:

Code:
$ IFS=","
$ STR="a,b,c"
$ A=( $STR )
$ echo "${A[0]}"
a
$

This was in BASH but should work in KSH too. Even an associative array will work.
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.
Quote:
Redirecting #0 and #1 is so common that you don't even have to specify the numbers explicitly. Redirecting #2 is less common but still very important. Redirecting any other number is (as far as I can tell) exceedingly rare. The only programs I know of that explicitly support it are screen and xterm - though obviously one could always write a program or shell script that uses a file descriptor specified numerically on the command line...
Don't forget the read builtin. Also, the linux flock utility. Also don't forget how any program taking a filename can get /proc/self/fd/23 shoehorned into it. Or you can just redirect anything you want into stdin. Once again, the shell's flexibility is key.

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.
Quote:
In practice it's rarely done.
That's a self-fulfilling prophecy -- partly because one file-descriptor is generally enough, and partly because of the syntax limits you want to fix. But I think it's very, very important to fix it in a way that doesn't rob the shell of any features it had before, and doesn't build any new walls.
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.
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.
Quote:
This is the basis of my argument that the current design of file operations in the shell is wrong. The case where one of those jobs actually does use that FD is the exception,. not the rule - therefore I think it's more sensible for the syntax to reflect this.
It already does.
Quote:
Rather than opening a file on FD #7, running eight jobs that don't care about FD #7 and two that do, I think it's better to open a file on a dynamically-assigned FD that's not exported to child processes by default (close-on-exec or whatever) - and then explicitly pass that FD to the child process, either via numeric FD redirection or another mechanism, in those cases where it's needed.
...except for special files because they're special.

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.

I guarantee that if you obscure file descriptors behind anonymous handles, you'll have to kludge a way around that. Maybe not today, maybe not tomorrow, but soon, and for the rest of your life. Smilie See man fileno.

Last edited by Corona688; 03-24-2011 at 07:15 PM..
 

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 06:07 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy