command to record terminal does not work with bash - linux

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.

Related

Open editor from bash script and cat file to variable not working

Why doesn't this work? There has to be a very simple answer to this.
I have two bash scripts. The first script calls the second which opens an editor on a tmp file, once user quits, cat the file which will be assigned to a variable in the first script.
test-use-editor
#!/bin/bash
test=$(use-editor)
echo $test
use-editor
#!/bin/bash
TMP_MSG="/tmp/tmp_msg"
$EDITOR $TMP_MSG
cat $TMP_MSG
rm $TMP_MSG
If I call use-editor without assigning and $() the editor opens just fine, but if I try to assign it to $test it just hangs. The editor does start because I can see it in my processes, but it's in a subshell or something. How can I get it to open within the terminal which called the first script?
You need to pass tty on to use-editor, as thatotherguy suggested.
test-use-editor
#!/usr/bin/env bash
test=$(./use-editor $(tty))
echo $test
use-editor
#!/usr/bin/env bash
TTY=${1:-/dev/tty}
TMP_MSG="/tmp/tmp_msg"
$EDITOR $TMP_MSG < $TTY >& $TTY
cat $TMP_MSG
rm $TMP_MSG

Can I call an interactive script in orther bash?

For example,there is an interactive script that read user input his name.And then write the name in a file.
#! /bin/bash
read name
echo $name>>name.txt
If I can't change the interactive script , how can I use anothor bash to quote the interactive script?
For example,I want to write a bash that extracts the name from a text and then calls this interactive script.
So can I achieve my idea?
Use a pipe to combine output of one program with the input of the next program:
echo 'name from other script' | ./script1.sh
echo can be replaced with any other executable file or script:
$ cat >script2.sh
#!/bin/sh
echo 'first input'
echo 'second input'
^D
$ chmod u+x script2.sh
$ ./script2.sh | ./script1.sh
If your intended input name is already in a file, use IO redirection instead:
$ ./script2.sh < file_containing_name.txt

How to execute commands read from the txt file using shell? [duplicate]

This question already has answers here:
Run bash commands from txt file
(4 answers)
Closed 4 years ago.
I tried to execute commands read it from txt file. But only 1st command is executing, after that script is terminated. My script file name is shellEx.sh is follows:
echo "pwd" > temp.txt
echo "ls" >> temp.txt
exec < temp.txt
while read line
do
exec $line
done
echo "printed"
if I keep echo in the place of exec, just it prints both pwd and ls. But i want to execute pwd and ls one by one.
o/p am getting is:
$ bash shellEx.sh
/c/Users/Aditya Gudipati/Desktop
But after pwd, ls also need to execute for me.
Anyone can please give better solution for this?
exec in bash is meant in the Unix sense where it means "stop running this program and start running another instead". This is why your script exits.
If you want to execute line as a shell command, you can use:
line="find . | wc -l"
eval "$line"
($line by itself will not allow using pipes, quotes, expansions or other shell syntax)
To execute the entire file including multiline commands, use one of:
source ./myfile # keep variables, allow exiting script
bash myfile # discard variables, limit exit to myfile
A file with one valid command per line is itself a shell script. Just use the . command to execute it in the current shell.
$ echo "pwd" > temp.txt
$ echo "ls" >> temp.txt
$ . temp.txt

How to run shell/python script in shell script?

I'm testing my shell script and python script together, so I write a shell script to call these two script in loop like this:
while ((1)) ; do
/usr/local/bin/ovs-appctl dpdk/vhost-list
sleep 10
/usr/local/bin/ovs-appctl dpdk/virtio-net-show n-f879ac2f
sleep 10
/usr/local/bin/ovs-appctl dpdk/virtio-net-show n-434ab558
sleep 10
/usr/local/bin/ovs-appctl dpdk/virtio-net-show i-brpri-p
sleep 10
`sh ./env_check`
`cat output.txt`
sleep 10
`python vm_qga_tool /opt/cloud/workspace/servers/6608da87-e374-4796-adb1-8faa29f49e9a/qga.sock connect:172.16.0.1`
sleep 10
done
but the result report error:
test-dpdk-virtio-net-show.sh: line 13: Checking: command not found
test-dpdk-virtio-net-show.sh: line 15: {"session":: command not found
Line13 is cat output.txt, line15 is python vm_qga_tool ....
the output.txt is the result of line12 sh ./env_check, and the output of vm_qga_tool is like this:
{"session"...
so how to fix this? How to cat result of the shell script in shell script?
sh ./env_check line will be replaced by the output of the script ./env_check. Current shell will try to execute them as commands. In your case it fails as, luckily, bash couldn't interpret them as commands. This could have been a disaster. For example, if your script produced an output like rm -rf *, you can imagine what would happen.
If you want to display the result of ./env_check you have to prepend the line with echo, so that the output of ./env_check will be fed to echo.
echo `sh ./env_check`
Or if you want to capture the output into a variable you do the following.
out=`sh ./env_check`
Same goes to cat ... and python ...
However use of back-ticks is discouraged. Instead use $(...). (Read more here.)
...
sleep 10
echo $(sh ./env_check)
echo $(cat output.txt)
sleep 10
echo $(python vm_qga_tool /opt/cloud/workspace/servers/6608da87-e374-4796-adb1-8faa29f49e9a/qga.sock connect:172.16.0.1)
sleep 10
...
However, you don't need to do any of that anyway. If you don't need to capture the command output, you can directly invoke the scripts, instead of invoking through a sub-shell.
...
sleep 10
sh ./env_check
cat output.txt
sleep 10
python vm_qga_tool /opt/cloud/workspace/servers/6608da87-e374-4796-adb1-8faa29f49e9a/qga.sock connect:172.16.0.1
sleep 10
...
The issues occurred because your shell accepted output of sh ./env_check and python vm_qga_tool ... as commands, so it will try to run those commands.
To fix it you should eliminate "" symbol fromsh ./env_checkorpython vm_qga_tool ...`

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

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

Resources