including bash "script" command inside a script - shell

I need to set some variables in my shell and format the shell prompt before I do a certain task every time. Also I need to record my actions.
At the moment, I'm running a script, collecting all the information it needs (partly by user input) and it sets the new shell prompt and the variables. Then I start the recording with script $var-actionlog-$anothervar-1.log. The variables used are set during the call of my_script.sh. Then, since a new shell spawned by the script command, I need to call source my_script.sh again.
Is there a way that I can include the script-part directly in my_script.sh so that I don't have to enter everything twice?

You can use export in your initial setup script to make the local variables global. For example, your setup script, my_script.sh could be:
#!/bin/sh
# A startup script
export var="world"
export PS1="\u#\h \w> "
echo "Please enter a number, followed by [ENTER]:"
read num
export anothervar="$num"
script $var-actionlog-$anothervar-1.log
Then, once the script has started, to confirm you can run:
echo var=$var, anothervar=$anothervar
If you want to set a different shell prompt ($PS1) in your my_script.sh, to be used by the typescript, make sure that you are not setting $PS1 in ~/.bashrc. The typescript, script $var-actionlog-$anothervar-1.log, calls ~/.bashrc every time and will override your prompt variable, $PS1. To set your prompt for log-in shells only, set this in ~/.bash_login.

Related

.bashrc file is looping "script" command

I'm trying to set up a little shell script using the linux command "script" to log every input on my Kali Linux machine.
#!/bin/bash
now=$(date +"%m_%d_%Y_%H:%M:%S")
script /root/Logs/log_$now.txt
The script itself seems to work but i want to add it to the bash autostart, so whenever i open a terminal, my shellscript gets executed.
I tried adding it to my .bashrc file but when I open a terminal now, the script gets looped.
I added a simple "echo 'test'" script and it only starts once on terminal launch.
Adding the script to my .profile file and executing .profile manually works as intended, but as soon as i enter a script using the "script" command to my .bashrc, it gets looped.
Thank you in advance.
A new terminal window is one way of starting a new interactive shell, but so is running script. You only want to run script in the first case, not in every case.
script itself sets a variable in the environment to let you know if you are already in a shell started by script. Check for that variable before trying to run script again.
if [[ -z $SCRIPT ]]; then
now=$(date +"%m_%d_%Y_%H:%M:%S")
script /root/Logs/log_$now.txt
fi
The value of SCRIPT, if set, is the name of the file being logged to.
Alternatively, you can configure your terminal emulator to run script directly, rather than having it continue to open an ordinary interactive shell and you trying to alter its configuration.
The above applies to BSD script; for GNU script, you'll have to set such a variable yourself.
if [[ -z $SCRIPT ]]; then
now=$(date +"%m_%d_%Y_%H:%M:%S")
export SCRIPT=/root/Logs/log_$now.txt
script "$SCRIPT"
fi
The script(1) command opens a new interactive shell.
The file .bashrc runs on every interactive bash shell that is started, hence your infinite recursion.
If you want something to run only on the login shell, you put it into .bash_profile.
This should avoid the infinite recursion.

How to overwrite environment variables in mac using Shell Script

I have a set of environment variables that need to be set on the basis of the arguments specified in the shell script.
But the problem is that those variables are already defined in the bash profile
FOR EXAMPLE:
bash_profile has a variable called "KARAN":
export KARAN=/config/1
Now on running the shell script, this is what it should do:
export KARAN=/config/2 (Changed the bash profile's KARAN value to 2)
Your question is not clear. If your script needs to set the env var to a specific value just do so using export VAR=val. What I think you're asking is how to have a script modify the environment of the current shell. And that is impossible without the cooperation of both shells. That is because environment vars are inherited by child processes. But a child process cannot directly modify the environment of its parent process (or some other random process for that matter). To do so the two processes must coordinate the exchange of data. This is typically done by using the source command if the child process is a shell script. Or by having the child process write a series of export statements to stdout and having the parent shell capture and execute those statements. For example, let's say I have a script named set_env that looks like this
#!/bin/sh
echo export KARAN=/config_2
echo export VAR2=val2
The current shell would then do
eval $(set_env)
Note, however, eval is dangerous. I prefer to do this which is slightly safer:
set_env | source /dev/stdin
That, however, only works in shells like ksh and zsh. Due to how bash handles pipelines the source is actually executed in a child shell and therefore the vars won't be set in the current shell.
You can create a new Profile with all the new definitions. and then call the line below on top of your shell script. Similarly, you can create as many profiles as you want and use it.
source bash_profile_new

How to export or set a make variable from a shell file

I am running a shell script from make environment
I execute the script with input parameters as make variables:
/shell_script.sh $(make_var1) $(make_var2)
I process these variables in shell. I want to assign the result from a shell command to the make variable and export back to shell.
make_var=shell_command
How can I do this?
It is not trivial to change the parent environment of a shell-script, one approach is to echo the export statements and source the output of the script in your parent environment:
...
echo "export make_var1=${make_var1}"
...
and when you launch your script do it using eval:
eval $(./shell_script.sh $make_var1 $make_var2)
this is the approach taken by for example ssh-agent.
A second option is to source the script, in that case the script will be run line-by-line in the current shell:
. shell-script.sh
any export statements in the script will be run in the current shell.

Making a bash script switch to interactive mode and give a prompt

I am writing a training tool, it is written in bash to teach bash/unix.
I want a script to run to set things up, then to hand control to the user.
I want it to be easily runnable by typing ./script-name
How do I do this?
I.E.
User types: tutorial/run
The run-tutorial script sets things up.
The user is presented with a task. (this bit works)
The command prompt is returned, with the shell still configured.
Currently it will work if I type . tutorial/bashrc
There are several options:
You start script in the same shell, using source or .;
You start a new shell but with your script as a initialization script:
The first is obvious; I write a little bit more details about the second.
For that, you use --init-file option:
bash --init-file my-init-script
You can even use this option in the shebang line:
#!/bin/bash --init-file
And then you start you script as always:
./script-name
Example:
$ cat ./script-name
#!/bin/bash --init-file
echo Setting session up
PS1='.\$ '
A=10
$ ./script-name
Setting session up
.$ echo $A
10
.$ exit
$ echo $A
$
As you can see, the script has made the environment for the user and then has given him the prompt.
Try making it an alias in your ~/.bashrc file. Add this to the bottom of ~/.bashrc:
alias tutorial='. tutorial/bashrc'
Then close and re-open your terminal, or type . ~/.bashrc to re-source it.
To use this alias, simply call tutorial, and that will automatically get replaced with its alias, as though you had called . tutorial/bashrc.

bash shell in Cygwin

when I am in a Cygwin terminal, I can easily use the "source" command.
For example, let's say I have a file called "my_aliases.sh", that contains the following
#!/bin/bash -f
alias clear='cmd /c cls'
#unalias clear
Then on the Cygwin terminal, I can type
$source my_aliases.sh
And it just works fine, and whenever I type "clear", I can see that it works.
But I don't know why doing the same thing inside another shell script, and calling that shell script doesn't work.
For example, let's say that I have a file called "run_alias.sh", with the following content:
#!/bin/bash -f
#
a=`source my_aliases.sh`
b=`ls -ltr`
echo $a
echo $b
And when I try to run my file
$ ./run_alias.sh
It just doesn't do anything. For example, I can see that the command (b) takes place, but nothing happens for command (a).
But after I run "run_alias.sh", and type "clear", I get the following error:
$ clear
bash: clear: command not found
I even tried to change run_alias.sh as follows:
#!/bin/bash -f
echo `source my_aliases.sh`
But now when run run_alias.sh, and type clear, I get the exact same error message !!!
Any idea how to call the "source" command from some other shell script in Cygwin?
A child process cannot alter its parent's environment.
When you execute the run_alias.sh script, you launch a new bash process, which sources your alias file. Then the script ends, that bash process terminates and it takes its modified environment with it.
If you want your aliases to be automatically available, source it from your $HOME/.bashrc file.
Backticks create a subshell. The changes made to your environment in that subshell do not affect the calling environment.
Id you want your script (run_alias.sh) to have access to the environment in my_aliases.sh, call source directly.
source my_aliases.sh
b=`ls -lrt`
echo $b
and if you want the changes that run_alias.sh makes to its environment to propagate to it's parent, run source on the command line.
$ source run_alias.sh

Resources