Can I call an interactive script in orther bash? - linux

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

Related

Define environment variable in a subshell with heredoc

Background: testing a bash script inside a pod where default user does not have sudo rights so that user cannot user vim or nano to create a .sh file so I have to find a way around with cat << EOF >> test.sh.
I am doing some local test to make sure it's working properly first. Locally I am creating a file test.sh with nano. See below
#!/bin/bash
# test.sh
VAR="Test"
echo $VAR
When I cat it:
#!/bin/bash
# test.sh
VAR="Test"
echo $VAR%
I save test.sh , made it execuatble chmod +x test.sh and ran it with ./test.sh The output:
Test
Now when I try to mimic the same behavior in a bash heredoc instead this is the command I use:
cat <<EOF >> test.sh
#!/bin/bash
# test.sh
VAR="Test"
echo $VAR
EOF
I saved it and made it execuatble as well. The cat output is:
#!/bin/bash
# test.sh
VAR="Test"
echo
So obviously running it wouldn't work. The output null.
I think the issue I am facing is that the environment variable $VAR is not defined properly inside the subshell using heredoc.
When you write this:
cat <<EOF >> test.sh
#!/bin/bash
# test.sh
VAR="Test"
echo $VAR
EOF
...that $VAR is expanded by your current shell (just like writing something like echo "$VAR"). If you want to suppress variable expansion inside your heredoc, you can quote it (note the quotes around 'EOF'):
cat <<'EOF' >> test.sh
#!/bin/bash
# test.sh
VAR="Test"
echo $VAR
EOF
This inhibits variable expansion just like single quotes (echo '$VAR').

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

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

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