Re: how to implement script command in a .cshrc ( csh) (809 Views)
Reply
Frequent Advisor
sstan
Posts: 39
Registered: ‎04-27-2004
Message 1 of 25 (809 Views)

how to implement script command in a .cshrc ( csh)

hi,
i encounter error when try to implemented a script command in a .cshrc file.

the line i added in .cshrc is :

set tstamp=(`date +%d%m%Y-%H%M%S`)
set fscript=/sgx/core/tslog/log-$tstamp
script $fscript
#echo $tstamp

Is the way doing is wrong for .cshrc ? any help is appreaciate. i canot figure what went wrong.

attached the 1st part of the .cshrc contents and the next part separated by line is the error shown when i login as the particular user .cshrc profile
Honored Contributor
Patrick Wallek
Posts: 13,774
Registered: ‎06-21-2000
Message 2 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

Why anyone would willingly use the c shell is a mystery to me. Anyway, try this:

setenv tstamp `date +%d%m%Y-%H%M%S`
setenv fscript /sgx/core/tslog/log-$tstamp
script $fscript
Honored Contributor
Patrick Wallek
Posts: 13,774
Registered: ‎06-21-2000
Message 3 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

After looking at your attachment, I'm confused.

Did you just log into the system as a regular user when the output was generated? I'm not sure why you get the multiple iterations of the script command.
Frequent Advisor
sstan
Posts: 39
Registered: ‎04-27-2004
Message 4 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

hi Patrick,
csh was used because legacy, then can the user variable assigned using the setenv?

I as a root user, then i do su - user and run the .cshrc and get the script command running repeately. i am wonder why ? also some error
Warning: ridiculously long PATH truncated

any idea ?
i try your method it still don't work.
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 5 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

ok, when you start a csh, it runs .cshrc. when your .cshrc runs, the "script" command starts a new shell (either /usr/bin/sh or the contents of $SHELL if avail). In your case, $SHELL /usr/bin/csh. You haven't completed your login, so the new csh runs .cshrc which starts a new shell which runs .cshr which......

PATH keeps getting added to, no logins get finished, and so on.

Still not sure why you'd want to use csh. I'm not sure what you mean by "Its legacy"
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 6 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

BTW, it tried the above in csh and its doing exactly as I described, altough it appears to function as desired in either ksh or sh.

note that if you do something like this in the other shells, the command immediately after the "script" command needs to be "exit", otherwise the user will remain logged in after they type exit the first time (and logging to the file will be shut off)
Frequent Advisor
sstan
Posts: 39
Registered: ‎04-27-2004
Message 7 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

hi patrick,
the script in .cshrc is seems working, but i noted a strange behaviour. When the 3 new added line executed, the alias ( shortcut ) in .cshrc missing when i do a su - user .

later when i type exit command it come back to the csh prompt with alias take effect ?

How to explain on this ? strange.
any advise is appreaciate.
bin is a shorcut to a path /sgx/core/bin

for e.g
$ pwd
/sgx/login/coreadm/RUN
$ bin
sh: bin: Execute permission denied.
$ exit
Script done, file is /sgx/core/tslog/log-01112007-033355
[~/RUN ](21)
[~/RUN ](21) bin
[/sgx/core/bin ](22)
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 8 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

from csh man page regarding command line switches:

-i Force csh to respond interactively when called from a device other than a computer terminal (such as another computer). csh normally responds non-interactively. If csh is called from a computer terminal, it always responds interactively, regardless of which options are selected.

note the part about "runs interactively". this implies that the .cshrc will always be run, so you are basically recursing.
Frequent Advisor
sstan
Posts: 39
Registered: ‎04-27-2004
Message 9 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

Hi OldSchool,

strange problem is when i su - username run the .cshrc ,
it say :

Script started, file is /sgx/core/tslog/log-01112007-035155
$

normally when user login it should show this type of prompt.
[~/RUN ](21)

BUT the csh prompt suppose to show is gone, and only when i type exit it return back and all alias is back to effect, how to overcome this problem. i try to put a exit after the script command , it show same symptom.

any advise is appreaciate. for e.g
bin is a shorcut to a path /sgx/core/bin

$ pwd
/sgx/login/coreadm/RUN
$ bin
sh: bin: Execute permission denied.
$ exit
Script done, file is /sgx/core/tslog/log-01112007-033355
[~/RUN ](21)
[~/RUN ](21) bin
[/sgx/core/bin ](22)
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 10 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

What I'm saying is:
A) I wouldn't / don't expect this to ever work as you want in csh.
B) It will work as desired if you change the users shell to either sh (POSIX) or sh
C) If you do B, then but "exit" after script in the users ".profile"

Frequent Advisor
sstan
Posts: 39
Registered: ‎04-27-2004
Message 11 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

hi Oldschool,
So there is no remedy for this issue ?

cause the .cshrc is run without user input. am i right ?
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 12 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

you might try placing your 3 lines of code in .login file in the user's home directory and see what happens.

I haven't used this shell in ~10 years, so I'm not real sure. This should get you close.
Acclaimed Contributor
Dennis Handly
Posts: 25,063
Registered: ‎03-06-2006
Message 13 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

>OldSchool: you might try placing your 3 lines of code in .login file in the user's home directory and see what happens.

This seems correct. If you insist on using the scummy C shell you need to know what each file does. .login is similar to .profile and .cshrc is similar to .kshrc (or whatever is in ENV).

I assume Avalanche wants to log the user's commands on login, not for every shell invocation.

>Patrick: Why anyone would willingly use the C shell is a mystery to me.

Exactly.

setenv tstamp `date +%d%m%Y-%H%M%S`
setenv fscript /sgx/core/tslog/log-$tstamp

Why would you suggest exporting those local variables? The only reasonable use is to stop the recursion by checking if tstamp exists. (You didn't suggest that.)
Frequent Advisor
sstan
Posts: 39
Registered: ‎04-27-2004
Message 14 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

HI Oldschool,

I had try to put the 3 lines in the .login of the user home directory, but result the same , it run the script command, the .cshrc export alias is back to normal when i type exit from the script. it basically same as what i describe early.

Acclaimed Contributor
Dennis Handly
Posts: 25,063
Registered: ‎03-06-2006
Message 15 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

>I had try to put the 3 lines in the .login of the user home directory, but result the same, it run the script command, the .cshrc export alias is back to normal when i type exit from the script.

What .cshrc export alias? You modified .login and you shouldn't have changed anything else.

And you will have to type "exit" twice. Once to exit script(1) and once to exit your scummy C shell. I suppose you can prevent that by using "exec script $fscript".

>it basically same as what i describe early.

You shouldn't have the recursion. Or PATH being too long.
Frequent Advisor
sstan
Posts: 39
Registered: ‎04-27-2004
Message 16 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

Hi Dennis,
Is there any way to overcome this problem? i want to achieve to logged the user login session into a file by doing the script command in csh , but it seems to be problem.

Had try to insert the script line in .login but it doesn't work, the .cshrc alias setting is lost when in invoke the script command, only when exit, the .cshrc alias setting is return normal, and once again i type exit to logout frm the session.

thanks.
Acclaimed Contributor
Dennis Handly
Posts: 25,063
Registered: ‎03-06-2006
Message 17 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

>the .cshrc alias setting is lost when in invoke the script command,

Ah, simple solution, source .cshrc in .login.

>once again i type exit to logout from the session.

Simple solution exec script(1) as I mentioned.
Frequent Advisor
sstan
Posts: 39
Registered: ‎04-27-2004
Message 18 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

hi Dennis,
>What .cshrc export alias? You modified .login and you shouldn't have changed anything else.
---> .cshrc export the alias of shortcuts to directory, like
alias bin "cd /abc/df/bin"
alias run "cd /abc/login/bh/RUN"
So you mean i only need to put the 3 line in .login or .cshrc ? i try put in .login it doesn;t work, the script kept recursively output the filename with diff timestamp.

>And you will have to type "exit" twice. Once to exit script(1) and once to exit your scummy C shell. I suppose you can prevent that by using "exec script $fscript".
--> so it mean i insert the line "exec script $fscript" in .login or .cshrc file.


>it basically same as what i describe early.
You shouldn't have the recursion. Or PATH being too long.
-----> when i put the 3 line in .cshrc it output the following in attached text file, same error :
Warning: ridiculously long PATH truncated
then cause many file with diff timestamp created, so i need to exit many time to get out from the shell prompt...
Frequent Advisor
sstan
Posts: 39
Registered: ‎04-27-2004
Message 19 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

hi dennis,
i put the line source /sgx/login/super1/.cshrc in .login file, then put the line "exec script $fscript" in .cshrc file.

But it output the same undesired result.
Any advise ?

a file atached with .login follow by .cshrc and a output of the action when i su - user as root and when it run the .cshrc once login.

thanks.
Acclaimed Contributor
Dennis Handly
Posts: 25,063
Registered: ‎03-06-2006
Message 20 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

>i put the line source /sgx/login/super1/.cshrc in .login file, then put the line "exec script $fscript" in .cshrc file.

Don't touch the .cshrc file. That's no end of problems.

Only change the end of .login to source the .cshrc file, followed by the the set commands, then at the end:
exec script $fscript
Frequent Advisor
sstan
Posts: 39
Registered: ‎04-27-2004
Message 21 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

HI Dennis,

i try the method put the "exec $fscript " at last line in .login, the .cshrc environment variable is source,
But i want to change the file ownership created by the script to root user so the user would not able to edit the file, but i insert the line :
/usr/bin/chown root:sys $fscript, and after i check the files ownership is not modified to be owned by root users, mayb once it something to do with "exec " behaviour ? anyway to overcome this, if i put the chown line before the script it will complain the file not found cause files haven;t created. Right.

source /sgx/login/super1/.cshrc
setenv tstamp `date +%d%m%Y-%H%M%S`
setenv fscript /sgx/core/tslog/log-$tstamp
exec script $fscript
/usr/bin/chown root:sys $fscript
Acclaimed Contributor
Dennis Handly
Posts: 25,063
Registered: ‎03-06-2006
Message 22 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

>But i want to change the file ownership created by the script to root user so the user would not able to edit the file

I assume this means everything else works? If so great!

>but i insert the line:
/usr/bin/chown root:sys $fscript, and after i check the files ownership is not modified to be owned by root users, maybe once it something to do with "exec" behaviour?

Where did you insert that line? (Ah after)

>if i put the chown line before the script it will complain the file not found cause files haven't created.
source /sgx/login/super1/.cshrc
setenv tstamp `date +%d%m%Y-%H%M%S`
setenv fscript /sgx/core/tslog/log-$tstamp
exec script $fscript
/usr/bin/chown root:sys $fscript

Hopefully another "simple solution". :-)
You can't have that line after exec since it does NOT return.

You can put it before:
touch $fscript
/usr/bin/chown root:sys $fscript
exec script $fscript

I assume you have changed or have the permissions so this user can write to it?

This is contradictory with your request:
>But i want to change the file ownership created by the script to root user so the user would not able to edit the file,

So the only way to do that is to redirect stdin to a file. And when you invoke script you need to redirect stdin to /dev/tty.

Forget that. Remove the eval and just do:
script $fscript
/usr/bin/chown root:sys $fscript
exit

I think script will read from stdin until "exit".
Then it will come back to the chown and exit.

Control C seems to be handled by script and won't be broken out of your jail. The only problem is the user can write junk to $fscript.

The only solution is to make $fscript a named pipe. At least he can't overwrite the beginning.
The pipe would have to have a root logger to continuously read from it.
Frequent Advisor
sstan
Posts: 39
Registered: ‎04-27-2004
Message 23 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

HI Dennis,
you are right .
If the user smart enough, before the file ownership change to root owner, they will modified the script file within the session before typing exit command, cause the chown only executed at the end of the " script " command exit.
Trusted Contributor
Tor-Arne Nostdal
Posts: 268
Registered: ‎08-17-1998
Message 24 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

Hi Avalanche.
This topic (using script command during login) have been discussed several times before...

See f.ex.
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1163248

It ain't a good idea, and you might create some workarounds.

Perhaps you could rather limit what the user is allowed to do (like with sudo)?

/2r
I'm trying to become President of the state I'm in...
Frequent Advisor
sstan
Posts: 39
Registered: ‎04-27-2004
Message 25 of 25 (809 Views)

Re: how to implement script command in a .cshrc ( csh)

thanks all.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.