Check if shell script is run via pipe - shell

my script should be run through a pipe like this:
echo "hello" | mysript.sh
I read the input with input=cat
However if it's run without a pipe cat waits for input - how can I fix this?

There is nothing to fix as it sounds like it works like every other utility that accepts input via stdin. That's a good thing.

Related

Monitor changes in stdin from a bash script

I would like to make a bash tool as a script to get notified when a change in the output of a given command happens. Use cases would be for instance to get a notification when the output of a long script changes. I would like it to be used on the same model as the tee command :
any_long_script_to_run | my_tool
The output would be then be transparently copied to stdout, but I would like to define as well a custom function (play a sound, display a notification...) to run each time a new line is written for instance.
Is there any clever way of doing this in bash ?
Thanks a lot !
The command to create a notification depends on what OS/window manager you're running, but one simple answer is:
any_long_running_command | while IFS= read -r line; do
printf "\a%s\n" "$line"
done
\a is the "bell" character, and usually makes a sound.

Send Ctrl + d to a server via bash

I'm working on a script, that requires you press control + d when you complete your entries. I'd like to send this command so I can just script my work rather than having to redo my work.
You're probably talking about the "end of transmission" delimiter which is used to indicate the end of user input. If that's the case then you can always pipe data into your script. That is, instead of this:
$ test_script.sh
My input!
^D
You'd write that data to a file:
$ cat > input
My input!
^D
Then pipe that into the script:
$ test_script.sh < input
No ^D is required because once that file is fully read the script is signalled accordingly. The < shell operator switches STDIN to read from a file instead of the terminal. Likewise, > can be used to capture the output of a program and save it to a file, as done in the second step here, though you can use any tool you'd like to create or edit that input file.
This works with pretty much any scripting language, from Python, Perl, Ruby to Node.js as well as bash and other shells.

rtorrent execute shell script

I can't figure out how to get output from shell script back to rtorrent after command has been executed.
Is it possible to return back output from exeternal command back to rtorrent session?
I use rtorrent scripting interface to auto execute shell command after torrent is finished
event line in .rtorrent.rc looks like this:
system.method.set_key = event.download.finished,mycustomcommand,"execute=~/myshellscript.sh"
myshellscript.sh file looks like this
#!/bin/sh
echo "Torrent finished!"
Is there a way to do this?
I'm not sure what you're searching for, but I found this on rtorrent's wiki site:
execute_capture_nothrow={command,arg1,arg2,...}
This will execute the external command with arguments arg1,arg2,.... It will return the
stdout output of the command.
system.method.set_key = event.download.finished,mycustomcommand,print="$execute_capture=/path/to/script"
should work, at least
print="$execute_capture=/path/to/script"
works when you do it inside rtorrent. If you want to store the output then intstead of print use d.custom1.set= if that helps.
You forgot to add parameters to the rtorrent.rc itself and also the bash script is incomplete according to me.
.rtorrent.rc line should have
method.set_key = event.download.finished,whatever,"execute2={/path/myscript.sh,$d.name=,$d.base_path=,$d.hash=}"
bash script
#!/bin/bash
TORRENT_NAME=1
TORRENT_PATH=2
TORRENT_HASH=3
touch "$1" Finished download!
exit
this will create touch file telling you particular file has finished downloading.

Is it possible to start a program from the command line with input from a file without terminating the program?

I have a program that users can run using the command line. Once running, it receives and processes commands from the keyboard. It's possible to start the program with input from disk like so: $ ./program < startScript.sh. However, the program quits as soon as the script finishes running. Is there a way to start the program with input from disk using < that does not quit the program and allows additional keyboard input to be read?
(cat foo.txt; cat) | ./program
I.e., create a subshell (that's what the parentheses do) which first outputs the contents of foo.txt and after that just copies whatever the user types. Then, take the combined output of this subshell and pipe it into stdin of your program.
Note that this also works for other combinations. Say you want to start a program that always asks the same questions. The best approach would be to use "expect" and make sure the questions didn't change, but for a quick workaround, you can also do something like this:
(echo y; echo $file; echo n) | whatever
Use system("pause")(in bash it's just "pause") in your program so that it does not exit immediatly.
There are alternatives such as
dummy read
infinite loop
sigsuspend
many more
Why not try something like this
BODY=`./startScript.sh`
if [ -n "$BODY" ]
then cat "$BODY" |./program
fi
That depends on how the program is coded. This cannot be achieved from writing code in startScript.sh, if that is what you're trying to achieve.
What you could do is write a callingScript.sh that asks for the input first and then calls the program < startScript.sh.

Switch from file contents to STDIN in piped command? (Linux Shell)

I have a program (that I did not write) which is not designed to read in commands from a file. Entering commands on STDIN is pretty tedious, so I'd like to be able to automate it by writing the commands in a file for re-use. Trouble is, if the program hits EOF, it loops infinitely trying to read in the next command dropping an endless torrent of menu options on the screen.
What I'd like to be able to do is cat a file containing the commands into the program via a pipe, then use some sort of shell magic to have it switch from the file to STDIN when it hits the file's EOF.
Note: I've already considered using cat with the '-' for STDIN. Unfortunately (I didn't know this before), piped commands wait for the first program's output to terminate before starting the second program -- they do not run in parallel. If there's some way to get the programs to run in parallel with that kind of piping action, that would work!
Any thoughts? Thanks for any assistance!
EDIT:
I should note that my goal is not only to prevent the system from hitting the end of the commands file. I would like to be able to continue typing in commands from the keyboard when the file hits EOF.
I would do something like
(cat your_file_with_commands; cat) | sh your_script
That way, when the file with commands is done, the second cat will feed your script with whatever you type on stdin afterwards.
Same as Idelic answer with more simple syntax ;)
cat your_file_with_commands - | sh your_script
I would think expect would work for this.
Have you tried using something like tail -f commandfile | command I think that should pipe the lines of the file to command without closing the file descriptor afterwards. Use -n to specify the number of lines to be piped if tail -f doesn't catch all of them.

Resources