When does a shell read into memory?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting When does a shell read into memory?
# 1  
Old 05-21-2002
When does a shell read into memory?

I don't know how to ask this clearly, so I'll try my best...

At what point does a shell script stop reading from a file, and begin using the buffered script?

Hopefully, this example will shed some light:

until [ "$value" = "quit" ]; do
sleep 10
print "What is your value?"
#print "Value, please: \c"
read value

echo Hi
echo Lo

Now, if I execute this in the background, it will just wait for me.
During that time, if I edit tmp.sh, and uncomment the "Value Please" line, and comment out the "What is your value" line, it will continue with "What is your value".
If I comment out the "echo Lo" line, before I type "quit", the change is effective when the script completes.

As I understand it, the shells reads the loop into memory, and loops away... It doesn't seem to read the rest of the script in until I get to that part of the script...

Is there some documentation on this (as far as which circumstances will cause the shell to read ahead or not)?

I can provide other examples, if this one makes no sense (well, as much as it could, considering the silliness of a self-rewriting script, or the dangerous nature of modifying a script that is currently running....)
# 2  
Old 05-21-2002
Dave Korn's book on ksh fully documents this behavior with respect to his shell. I am, of course, properly aghast that you would edit a running shell script. But this has implications with aliases which are processed when a command is read. I don't have the book in front of me, but here is an approximation of one of his examples:
#! /usr/bin/ksh
alias xxx="echo one"
if [[ 1 != 0 ]] ; then
       alias xxx="echo two"
exit 0

This will print:

When I use aliases in a complex way, I will just fall back on eval. Replacing the first xxx command with "eval xxx" yields the expected result.

I'm not sure if I trust my memory on this, but I think that compound commands are read completely before execution. And, IIRC, so are dot scripts.

By editing a running script, you are going to stumble on two additional issues though. The first is buffering. I doubt very much that ksh issues individual read() system calls until it exactly acquires the last new line character that completes it's current compound command. Instead, I would guess that an unknown number of other characters have been pre-read into a buffer awaiting the next read. And this could easily be implementation dependent. The next problem is varying line length. You seem to be envisioning that ksh remembers the last line number read and somehow will resume with the line that follows. Instead ksh will be positioned on a byte number and will read the next byte. You could easily throw it into the middle of a line.
# 3  
Old 05-21-2002
You seem to be envisioning that ksh remembers the last line number read and somehow will resume with the line that follows. Instead ksh will be positioned on a byte number and will read the next byte. You could easily throw it into the middle of a line.
Which makes sense, as I have seen it do some wierd stuff when playing with this the other day...
One of the first things we noticed (I wish I could remember what script we were playing with, since it exhibited this behavior), is that I could have a valid command, like "echo hello", that after changing, would cause the script to error out, complaining:
./tmp.sh: beho: command not found
We tried to control which bytes it overwrote, but were unable to control it in any usable manner...
# 4  
Old 05-21-2002
Hmm, and for some reason, that script doesn't work in bash... It says "xxx: not found" for both of them...

Perplexed as ever Smilie
# 5  
Old 05-21-2002
For bash, it looks like you need to add a line to the the top of the script to enable aliases:
shopt -s expand_aliases
# 6  
Old 05-22-2002
From the man page:
Aliases are not expanded when the shell is not interactive, unless the
expand_aliases shell option is set using shopt (see the description of shopt under
I never realized that before now. I guess it's since I can't think of a time when I've ever used aliases in a bash script...

In fact, I don't think I've ever used shopt before either, although I use disown every now and again...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Linux Shell Piping w/Shared Memory

So I am pretty new to the linux environment, and I am trying to create a shell that uses multiple pipes, and I read online that piping using shared memory space is more efficient than using regular piping. However, I have zero clue how to use shared memory space with pipes. Has anyone done this... (1 Reply)
Discussion started by: Greg_MC
1 Replies

2. Shell Programming and Scripting

Shell script for %computational memory & % non computational memory

Dear, How to calculate %computational memory and %non computational memory from AIX server. What command used to find out %computational memory and % non computational memory except topas. Regards Nowshath (1 Reply)
Discussion started by: Nowshath
1 Replies

3. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

4. Linux

Reserv memory for uid 0 / sshd+shell

Hi! Does someone know how to ensure that root user always will be able to login to a system? What I mean is when a server is out of RAM and swapping like hell; sshd may spawn a shell and login root user to save the day? (2 Replies)
Discussion started by: Esaia
2 Replies

5. Programming

Linux read and write external memory.

Hi everyone. I asked once here and it went well, so I'm very happy with this community. I have a new question to ask. I would like to read&write specify memory of a process which is not the process I'm doing it from, nor a child nor a parent. So, I want to do a program similar to Cheat... (3 Replies)
Discussion started by: lilezek
3 Replies

6. UNIX for Dummies Questions & Answers

Write/Read in Shared memory

I have created and attached a shared memory segment. However I do not know how am I supposed to write and read data from it. I want to save various different data on this segment such as process IDs and other. Do you know how am I supposed to write these and be able to read such data in the correct... (1 Reply)
Discussion started by: tyron
1 Replies

7. Programming

Urgent!!!! - Write/read to/from shared memory

I created a shared memory and attached it. I also created the appropriate semaphores to sync. my read/write operations. However I do not know how am I supposed to do so. Anyone has got any ideas? I want to write int the form of 1234:23:444:... where each number between : means something to the... (0 Replies)
Discussion started by: tyron
0 Replies

8. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

9. Programming

shared memory read/write using threads

I am looking for C program source code. Could you please help me in finding the source code required mentioned below. program to create multiple threads (one master thread and rest worker threads) and using the threads write into and read from shared memory Restrictions: Only one thread... (2 Replies)
Discussion started by: kumars
2 Replies

10. Shell Programming and Scripting

Find memory leaks shell script ???

Hi all, Has anyone out there a shell script to detect memory leaks on unix machines? And if so what way did they go about it .? (5 Replies)
Discussion started by: nano2
5 Replies
Login or Register to Ask a Question