Put Script on Start-up the sh Terminal

Hi all
I want to put this script on start-up the sh Terminal to save history of output:

if [ -z "$UNDER_SCRIPT" ]; then       
        logdir=$HOME/terminal-logs
        if [ ! -d $logdir ]; then
                mkdir $logdir
        fi
        gzip -q $logdir/*.log
        logfile=$logdir/$(date +%F_%T).$$.log
        export UNDER_SCRIPT=$logfile
        script -f -q $logfile
        exit
fi
  

But I can't find how to do that. I should only use the sh shell. yes in bash shell there is some way, but how about sh?
I alter the files profile .dtprofile and I create .profile on root directory and changed all of them and add this codes:

 echo Rahim
ls -a
Rahim=123
echo $Rahim

But any of them didn't work.
Is it possible to put Script on start-up the Terminal to execute every time that a Terminal starts? If possible please let me know.
I use x86 sun Solaris 10.

Thanks for your helps.

Q.1:
How do you connect?

If you connect via a Xserver ( emulator like Rx or Hummingbird...), and use ssh, do you know which profile is being used?... (Look at the man pages of ssh...)

1 Like

Are you talking of local on a workstation ?

In any case the best is to put your code in a script and call the script in the good .profile using exec otherwise you will find yourself with the prompt waiting yet another exit since script forks a new shell...

1 Like

Now it is on my virtual box and it is local. But on the office I connect Using Xserver.
Sometime we use telnet, But that's not so important. When we connect from Xserver to desktop of the Solaris I need to do that. I can't find the .profile file in home, then I create one but nothing! I think echo command maybe not worked.
then you said to me I should call my Script with the exec command on .profile?
OK let me try.
Thanks for your help.

There is an importance: using telnet you will read .profile, using ssh you will not unless you create a environment file in .ssh directory specifying so...
I tested at work your bit of script : put in a script and called the script in .profile... works as expected :
Even though you put an exit at the end and I added an extra as last line of .profile when I type exit I found myself at the prompt so:
You must use my previous syntax using exec
I tested... works great...

But not for me!
I am so new on this. I should try. but another question?
Where is the .profile location? I didn't have any! I create one in the / directory. Am I right?

---------- Post updated at 11:54 PM ---------- Previous update was at 11:43 PM ----------

It give me the error cannot execute
I exec it directly from Terminal with this

./Savelog

this was the second question! Am I right?

Is that literally what it said, and literally all it said?

It might be better placed in the shells startup code, that is: ~/.kshrc or ~/.bashrc , depending on which shell thread-o/p is using. Unlike the .profile , which is run every time a user logs in, the rc-file is executed every time a shell instance is started. This will happen automatically whenever a terminal emulator, such as xterm , is started.

I hope this helps.

bakunin

Yes, that is.

./Savelog: cannot execute

Maybe I need to set environment variable? I don't know. I think first of all I should run the Script manually and then try to put it on start-up.
As I said I didn't have any .profile file, I create one manually on the root.

---------- Post updated at 07:00 AM ---------- Previous update was at 06:52 AM ----------

Thanks for your reply bakunin
But as I said the shell is sh shell not bash or ksh.
I condemn to use only and only sh shell!

Let me the time to configure a server where I can show where Im coming from

ran01:/home/vbe $ ssh dany1
Password: 
Last login: Fri Jun  7 11:19:54 2013 from ran01.c*i.et*
Sun Microsystems Inc.   SunOS 5.10      Generic January 2005
 Enter DISPLAY =                                                                

gzip: /export/home/vbe/terminal-logs/*.log: No such file or directory
logfile is : /export/home/vbe/terminal-logs/2013-06-07_11:20:37.21176.log

usage: script [ -a ] [ typescript ]  # So I corrected the script...
Connection to dany1 closed.
ran01:/home/vbe $ ssh dany1
Password: 
Last login: Fri Jun  7 11:20:36 2013 from ran01.c*i.et*
Sun Microsystems Inc.   SunOS 5.10      Generic January 2005
 Enter DISPLAY =                                                                

gzip: /export/home/vbe/terminal-logs/*.log: No such file or directory
logfile is : /export/home/vbe/terminal-logs/2013-06-07_11:27:51.26677.log
Script started, file is /export/home/vbe/terminal-logs/2013-06-07_11:27:51.26677.log
dany1:/export/home/vbe $ ll
sh: ll:  not found
dany1:/export/home/vbe $ r alias
alias ll='ls -al'
dany1:/export/home/vbe $ ll
total 38
drwxr-xr-x   4 vbe      sysdso         9 Jun  7 11:27 .
drwxr-xr-x  21 root     root          21 Jun 10  2011 ..
-rwx------   1 vbe      sysdso      6651 May 27 14:06 .kshrc
-rw-r--r--   1 vbe      sysdso       725 Jun  7 11:20 .profile
-rw-------   1 vbe      sysdso      3056 Jun  7 11:28 .sh_history
drwx------   2 vbe      sysdso         3 Jun  7 10:45 .ssh
-rw-r--r--   1 vbe      sysdso        50 Dec  7  2007 dsmerror.log
-rwx------   1 vbe      sysdso       419 Jun  7 11:26 script_shell
drwxr-xr-x   2 vbe      sysdso         3 Jun  7 11:27 terminal-logs
dany1:/export/home/vbe $ tail .profile
then
   echo " Enter DISPLAY = " 
   read DISPLAY
   export DISPLAY
fi
alias -x ll='ls -al'
export PS1=`uname -n`':$PWD \$ '

printf  "\033]0;$(hostname) \007"
exec $HOME/script_shell
dany1:/export/home/vbe $ exit
Script done, file is /export/home/vbe/terminal-logs/2013-06-07_11:27:51.26677.log
 exiting ...
Connection to dany1 closed.
ran01:/home/vbe $ 

The what will follow I have an issue to solve sorry...

Here you are:
This is for the case you get to your host by ssh...

#----------------
dany1:/export/home/vbe $ ll term*
total 10
drwxr-xr-x   2 vbe      sysdso         3 Jun  7 11:27 .
drwxr-xr-x   4 vbe      sysdso         9 Jun  7 11:27 ..
-rw-r--r--   1 vbe      sysdso      1073 Jun  7 11:28 2013-06-07_11:27:51.26677.log
dany1:/export/home/vbe $ ll .ssh/environment; cat .ssh/environment;
-rwx------   1 vbe      sysdso       192 Jun  7 10:45 .ssh/environment
# Set up the shell environment:
        TMOUT=0
# Set up the shell variables:
        EDITOR=vi

        #ENV=$HOME/.kshrc
        ENV=$HOME/.profile
HISTFILE=$HOME/.sh_history
HISTSIZE=10000
dany1:/export/home/vbe $ 
dany1:/export/home/vbe $ ll script_shell ; cat script_shell
-rwx------   1 vbe      sysdso       419 Jun  7 11:26 script_shell
if [ -z "$UNDER_SCRIPT" ]; then
        logdir=$HOME/terminal-logs
        if [ ! -d $logdir ]; then
                mkdir $logdir
        fi
        gzip -q $logdir/*.log
        logfile=$logdir/$(date +%F_%T).$$.log
        export UNDER_SCRIPT=$logfile
        echo logfile is : $logfile
        #read kyb
        #script -f -q $logfile
        script -a $logfile
        echo " exiting ..."
        sleep 1
        exit
fi

dany1:/export/home/vbe $ cat term*/*   # I am looking at your log content  (hehe you see it works...)
Script started on Fri Jun 07 11:27:51 2013
dany1:/export/home/vbe $ ll
sh: ll:  not found
dany1:/export/home/vbe $ r alias
alias ll='ls -al'
dany1:/export/home/vbe $ ll
total 38
drwxr-xr-x   4 vbe      sysdso         9 Jun  7 11:27 .
drwxr-xr-x  21 root     root          21 Jun 10  2011 ..
-rwx------   1 vbe      sysdso      6651 May 27 14:06 .kshrc
-rw-r--r--   1 vbe      sysdso       725 Jun  7 11:20 .profile
-rw-------   1 vbe      sysdso      3056 Jun  7 11:28 .sh_history
drwx------   2 vbe      sysdso         3 Jun  7 10:45 .ssh
-rw-r--r--   1 vbe      sysdso        50 Dec  7  2007 dsmerror.log
-rwx------   1 vbe      sysdso       419 Jun  7 11:26 script_shell
drwxr-xr-x   2 vbe      sysdso         3 Jun  7 11:27 terminal-logs
dany1:/export/home/vbe $ tail .profile
then
   echo " Enter DISPLAY = " 
   read DISPLAY
   export DISPLAY
fi
alias -x ll='ls -al'
export PS1=`uname -n`':$PWD \$ '

printf  "\033]0;$(hostname) \007"
exec $HOME/script_shell
dany1:/export/home/vbe $ exit

script done on Fri Jun 07 11:28:43 2013
dany1:/export/home/vbe $ 

In other words if you telnet , remsh or rlogin, the append in .profile is enough, if you get to remote via ssh, you will have to create if you have no file environment as I show you above.
By X means to correct .dtprofile to make it read .profile:
bottom of .dtprofile:

# #  login screen, login and correct the error. The $HOME/.dt/startlog and
# #  $HOME/.dt/errorlog files may be helpful in identifying errors.
# #
# ##########################################################################

#
#  If $HOME/.profile (.login) has been edited as described above, uncomment
#  the following line.
#
 DTSOURCEPROFILE=true

ADDENDUM:

Just read your last question:
exec here I use exec since when the command script ends, the forked shell exits, and you find yourself at your login prompt... which I suspect is not what you want.
exec will make the forked shell, the current shell that will at exit end the session... as you can see on my first code trace above

You said you use "sh shell". This can mean one of two things: a POSIX shell or a Bourne Shell. If it is a POSIX shell you can set the variable "ENV" pointing to some startup file. This would be called every time a new shell starts. If it is a Bourne Shell, then you can use the startup option of the terminal: in case of "xterm" use the "-e" option to specify a program to be run at terminal startup.

I hope this helps.

bakunin