How do I exit pbcopy in the Mac Terminal? - macos

If I just enter pbcopy by itself in Terminal, it appears to prompt for user input. I'm assuming that the idea is you enter some input, then exit or end pbcopy, and then your input is added to the clipboard, so you can pbpaste it or whatever.
I can't figure out how to end pbcopy, without using Control-C which kills the process and doesn't save my data to the clipboard.
Still very new to Terminal. Thanks!

For most terminal programs, end of input is triggered by Ctrl-D. This is considered the valid conclusion of input from the command.
It could be something else, but the key combination can be read from the output of stty -a, which shows an entry: eof = ^D, which indicates Ctrl-D is that key combination.
Now programatically, if you're trying to get input into pbcopy, you can do, from a file:
cat file | pbcopy
or the slightly more 'shell purist' (doesn't waste a process with the cat):
pbcopy <file
from a bash script, using a here document:
pbcopy <<EOM
copy copy copy...
EOM

Related

passing a string to open command in mac

I want to pipe a string to the open command in zsh terminal.
this command: open https://www.google.com open the web-browser correctly.
However, running this command: echo https://www.google.com | open does not work. What's the correct way to pipe a string to this command?
As far as I can see from the Man Page the open command does not take input from stdin so piping into it does not make much sense, but your syntax for piping is correct. You probably want to pass the result of echo as an argument.
Try:
open $(echo https://www.google.com)

Is there a way to simulate Save and Close a file through shell command?

I have a scenario where a shell script runs another shell script. The second shell script prompts user to type review comments and close the file.
In my case, I do not want to type anything but only save and quit (equivalent vim command :wq) the file. I want to automate this through a shell script where I don't want manual intervention to save and quit the file. How can i achieve this?
I'm not sure i get the context but if you simply (echo "input of your choice" >> file.txt ) it will do the job and will not have to quit and save anything
if th einput is already in a file you can (cat inputfile.txt >> file.txt )
this command can be place inside of your first script

Capture output of last executed command into a variable without affecting Vim and line returns

From this question: bash - automatically capture output of last executed command into a variable I used this command:
PROMPT_COMMAND='LAST="`cat /tmp/x`"; exec >/dev/tty; exec > >(tee /tmp/x)'
It works, but when I use Vim I get this:
# vim
Vim: Warning: Output is not to a terminal
Then Vim opens. But it takes a while. Is there a way to get rid of this message and the slowdown?
Also when I list dir and I echo $LAST it removes the return lines (\n). Is there a way to keep the return lines (\n)?
I think what you ask for is hard do achieve. Vim tests if the output is a terminal. The command you've provided redirects the output to the tee command. tee saves its input (which also menans: command's output) to the file and outputs it to the terminal. But vim knows nothing about it. It only knows its output is not a terminal. So it outputs warning. And from the vim's source code:
[...]
if (scriptin[0] == NULL)
ui_delay(2000L, TRUE);
TIME_MSG("Warning delay");
which means this redirection will always get you 2 seconds delay.
Also, for example, man vim command will not work with such redirections, because terminal output has some attributest (e.g. width and height) which generic file hasn't. So... it won't work.

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.

Can AppleScript take output of shell script and put in paste buffer?

Can AppleScript take the output of a shell script or variable and put it in the paste buffer?
I have a file with a password for every day (formatted "date,password") and I want to write a script that when run will look up the date and output the password for that date.
That part is not a problem, I'm just wondering if there is a way to get the output to automatically go into the paste buffer?
Using AppleScript to put output of a shell command into the clipboard:
set the clipboard to (do shell script "ls")
If you do not want to use AppleScript you can use pbcopy in the shell:
sh$ some command | pbcopy

Resources