lp is not working in a bash script - bash

I have a test script that contains the following:
lp -d HP ~/cap/alpha/error
HP is a designated printer on my wireless network and ~/cap/alpha/error is the full file path to the file containing the report from a python unit test.
The command works from the terminal window but not from the script called test.
I can't figure out why this is not working?

How to pipe stdout while keeping it on screen ? (and not to a output file) (answer by jilliagre):
echo 'ee' | tee /dev/tty | foo
This answer worked for me as follows:
$ python3 "mytest.py" | tee /dev/tty/ | lp -d brother
printing to both my terminal and my printer.

Related

Batch (Windows) alternative for Bash | tee -a log.txt

I need to get stdout echoed onto the command prompt and appended into a file.
I have tried echo "foo" | tee -a log.txt. I have looked on google, however these is nothing there that is relevant.
"foo" | tee -a log.txt
It should echo foo and append it to a file. Instead I get
'tee' is not recognized as an internal or external command,
operable program or batch file.
I don't want the command tee, I need to get stdout echoed onto the command prompt
Pass what your trying into power shell, like this:
powershell "echo foo | tee -a foo.txt"

List specific files on a remote windows machine from a linux machine using sftp

working a script that lists or outputs specific files on a windows machine directory when executed a script from a Linux server
Here is the current script we got, it lists all contents in the windows directory but my requirement is to filter out the required ones
echo "ls -l ${TARGET_ICT_DIR}\nquit\n" | sftp ${SFTP_LOGIN} | tee /tmp/ver_ict_rel_dest_${L_CURR_PID}.txt
The above command outputs all files in that windows directory but im looking for files that only searches below files:
anchor.jar
rename.txt
zipper.dat
Please suggest
Will this insertion of grep in the pipeline do the trick?
echo "ls -l ${TARGET_ICT_DIR}\nquit\n" |
sftp ${SFTP_LOGIN} |
grep -E ' (anchor\.jar|rename\.txt|zipper.dat)\r?$' |
tee /tmp/ver_ict_rel_dest_${L_CURR_PID}.txt
Do not know if the check for \r is necessary -- but... Windows... Also, in comment #ghoti noted that \r might not translate well to all versions of grep and shell. You could try something like the following if you run into such:
echo "ls -l ${TARGET_ICT_DIR}\nquit\n" |
sftp ${SFTP_LOGIN} |
grep -E " (anchor\.jar|rename\.txt|zipper.dat)$(printf '\r')?$" |
tee /tmp/ver_ict_rel_dest_${L_CURR_PID}.txt
(Both variants of the ls -l | grep -E ... part was tested with dash and bash on Linux.)
i used below one and it satisfied my condition
echo "ls -l {anchor.jar,rename.txt,zipper.dat} ${TARGET_ICT_DIR}\nquit\n" | sftp ${SFTP_LOGIN} | tee /tmp/ver_ict_rel_dest_${L_CURR_PID}.txt

Write command output to file plus console from another console

I am developing a script where I have to run it from the command line (say T1). The script has to open another terminal (T2) and the output of this terminal (T2) has to be redirected to a file, so that I can parse the file from the main script (T1). I know how to open a new terminal (T2) from the main terminal (T1).
gnome-terminal -e "ant" 2>&1
I also know how to throw command output to file plus console by using tee
ls | tee /home/xyz.txt
So I try to run T2 from T1 and redirect T2's output to xyz.txt by doing this:
gnome-terminal -e "ant" 2>&1 | tee /home/xyz.txt
However xyz.txt doesn't get output from T2.
So how to get the output of T2 to xyz.txt from T1?
While this sounds very convoluted and looks like an XY-Problem, here's one way to do it (tested with xterm instead of gnome-terminal).
gnome-terminal -e "ant | tee $(tty) xyz.txt; read dummy"
The tty names the terminal device where you start the terminal, not the new terminal. The read is optional and waits for ENTER so you see what's on the terminal display.

bash command preserve color when piping [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can colorized output be captured via shell redirect?
setup
In this case specifically I'm trying to preserve the colors in git status -s when piping it to another command.
Some git commands, diff for instance, and other commands like grep have an option --color=always but git status does not.
question
Is there a way to pipe or capture the output of a command and make it think it is outputting to the xterm shell so it doesn't automatically disable colors?
Here's a script snippet using the colorized output of ls as an example (on Mac OS X 10.6).
# no colored ls output if stdout is a pipe (and not a tty)
ls -G /
ls -G / | cat
script -q /dev/null ls -G / | tr -d '\r' | cat
# write output of script command to a variable
var="$(script -q /dev/null ls -G / | tr -d '\r' | cat)"
echo "$var"
Most commands that do print out those color codes explicitly check if stdout/stderr is a tty (using the isatty function).
If you want to preserve the color codes, you can run it within a terminal emulator like screen or the direct logger script, saving the output to a file.

A simple way to add pipes to tee-d file

I'm trying to record the output of a command with post processing to clean things up
(like removing ansi escape codes to a file while outputing the command to screen)
(command is minicom which functions as a terminal among other things).
currently I have the following but it doesn't work(seems to block).
rm "${fifo}"
mkfifo "${fifo}"
cat "${fifo}"|filter_1 >"${log_file}" &
command |tee "${fifo}"
p.s.
command | tee "${log_file}"
works fine
Besides unbuffer, you can try
{ command ; printf "\n" ; } | tee "${log_file}"
I hope this helps.

Resources