Csh script won't exit when it was invoked by a Bash script - linux

I wrote a Bash script to invoke a Csh script. After the Csh script finishing, it will not exit, even an "exit" command was appended in the last of the file. The following are scripts:
$ cat test.sh
#!/bin/sh
./test1.csh
$ cat test1.csh
#!/bin/csh
echo "I'am test1.csh"
exit
When I run the "test.sh", the Csh script did not exit.
Using ps -ef | grep test, found that the process is still running, as shown in this screenshot

Related

Find all running processes with a certain name linux

I am new to linux, and I want to write to a .txt file all of the running processes on my PC that has the word "con" in them.
The script I wrote:
#!/bin/bash
ps -A | grep "con" > con_proc.txt
Why is this not working?
#!/bin/bash
ps -eaf | grep -i "con" > con_proc.txt
If you want to place inside of a script the contents of the script would be the above contents, for example script.sh.
To invoke the script you will need to do the following:
chmod +x script.sh
./script.sh
The first command gives the script execute permissions and the second command invokes the script.
Linux has pgrep to do this.
$ pgrep -a con
...

Get PID of last executed command (NO BACKGROUND)

I want to know the PID of the last executed command.
I saw this a lot:
$ command &
$ pid=$!
But I'm searching for the same thing without running the command in the background.
You can use the following construction in scripts:
PID=`sh -c "echo $$; exec your_command -with-arguments"`
echo $PID

Shell file is not invoking from other shell file

I have a.sh and b.sh. I changed shell from bash to ksh. Now it is not invoking b.sh.
a.sh
`#!/bin/ksh
source /home/ec2-user/env
abc_job() {
nohup abc >> $HOME/a.log 2>&1 </dev/null &
}
abc_jbo() >> $HOME/a.log
exit 0`
abc is the binary file of c that invokes b.sh
b.sh
`#!/bin/ksh
echo "completed b.sh job >> $HOME/b.log
exit`
The csh built-in command 'source' has the ksh equivalent '.', in your case:
. /home/ec2-user/env
If execution of /home/ec2-user/env is mandatory you might want to be more defensive and verify that it is present and executable and that it completes successfully.

command to record terminal does not work with bash

I would like to use "script" command, I have the following code
#!/bin/bash
script &
wait
echo "hello"
echo "hello2"
pid=$(pidof script | awk '{print $1}')
kill -9 $pid
I need the script command to capture the output, but after the command "script &" the output is :
Script started, file is typescript
Script done, file is typescript
and script does not record nothing, any idea of why?
This is how you should do it:
script <output-file> <commands>
Example:
script typescript bash -c 'echo "hello"; echo "hello2"'
Script started, output file is typescript
hello
hello2
Script done, output file is typescript
Then check output file created:
cat typescript
Script started on Sat Dec 19 01:54:04 2015
hello
hello2
Script done on Sat Dec 19 01:54:04 2015
There are two ways you can use the script command :
Save only the outputs of your code (i.e. batch mode)
$ script filename bash -c 'echo foo; echo bar'
which will output
Script started, file is filename
foo
bar
Script done, file is filename
Save all what is displayed on your terminal (i.e. interactive mode). To end the scripting, just type exit or hit Ctrl-D
$ script filename
Script started, file is filename
$ echo foo
foo
$ echo bar
bar
$ exit
exit
Script done, file is filename
Note that the batch way is a hack on the interactive classical way of using script.
In your case, just forget about the & and kill stuff and hit Ctrl-D when you want the script to end.

Difference between executing a script with 'bash cd.sh' and 'source cd.sh'? [duplicate]

This question already has answers here:
What is the difference between using `sh` and `source`?
(5 answers)
Closed 7 years ago.
Explain the difference between executing a script with bash cd.sh and source cd.sh
cd.sh contains:
#!/bin/sh
cd /tmp
bash execute the script in a child shell that cannot modify the environment of the invoking shell while source executes the script in the current shell:
test.sh
#!/bin/sh
export MY_NAME=chucksmash
echo $MY_NAME
Running test.sh:
chuck#precision:~$ bash test.sh
chucksmash
chuck#precision:~$ echo $MY_NAME
chuck#precision:~$ source test.sh
chucksmash
chuck#precision:~$ echo $MY_NAME
chucksmash
chuck#precision:~$
In bash, commands that look like source script.sh (or . script.sh) run the script in the current shell, regardless of the #! line.
Therefore, if you have a script (named script.sh in this example):
#!/bin/bash
VALUE=1
cd /tmp
This would print nothing (because VALUE is null) and not change your directory (because the commands were executed in another instance of bash):
bash script.sh
echo $VALUE
This would print 1 and change your directory to /tmp:
source script.sh
echo $VALUE
If you instead had this script (named script.py in this example):
#!/usr/bin/env python
print 'Hello, world"
This would give a WEIRD bash error (because it tries to interpret it as a bash script):
source shell.py
This would *also *give a WEIRD bash error (because it tries to interpret it as a bash script):
bash shell.py
This would print Hello, world:
./shell.py # assuming the execute bit it set

Resources