Shell script: Excecute a command in "screen" - linux

I use a USB-Dongle on my linux-server (raspberry).
To open the conecction and use the firmware, to do this, I have to open a terminal with "screen".
screen /dev/ttyACM0
There is no problem to open this connection with a shell script.
My problem is to execute a command in this screen.
The firmware has some command references for example V to get back the version number.
First of all, I tried a pipe (|)
screen /dev/ttyACM0 | echo "V"
But, this have no correct result.
Someone tell me I can use the screen command -X to execute something.
screen -T CUL /dev/ttyACM0
screen -X V
This seems to work.
The problem: "screen" gives an error message: -X: unknow command "V"
When I open the screen and entered the coammand V I get an output.
Some ideas?

Try this command:
screen -X stuff 'V'$(printf \\r)
(you also need the carriage return)

Related

Bash script with scrot area not working

I created a bash script to use for interactive screen capture and another one for window capture. I'm linking to these with keyboard shortcuts in Linux. The window capture script works without problems:
#!/bin/sh
scrot -u 'ScreenShot_%Y-%m-%d_at_%I:%M:%S-%p.png' -e 'mv $f ~/Pictures/scrot-screenshots'
But the script for for area capture (user selects area with mouse drag) does not work, even though the command works in terminal:
#!/bin/sh
scrot -s 'ScreenShot_%Y-%m-%d_at_%I:%M:%S-%p.png' -e 'mv $f ~/Pictures/scrot-screenshots'
What am I doing wrong? Or maybe a better question is what is preventing the script from letting me select an area of the screen?
I manged to get it working by adding a delay to give the giblib resource time (2/10 of a second) to load:
#!/bin/sh
sleep 0.2 ; scrot -s 'ScreenShot_%Y-%m-%d_at_%I:%M:%S-%p.png' -e 'mv $f ~/Pictures/scrot-screenshots'
How I found the solution:
I couldn't figure out how to get errors to output to a file because running my script from terminal didn't produce any errors. Double clicking the script ran properly and script > file 2>&1 in terminal didn't give me any errors because it ran properly from terminal. I only had errors when I tried to use the keyboard shortcuts (keybindings) attached to the second command from my original post. To see the error that finally lead to the above solution, I downloaded:
`apt-get install xbindkeys` && `apt-get install gconf-editor`
I ran gconf-editor and used the Run Action to executed the script the same manner it would be executed if I was using the keybindings...but attached to a terminal output. That gave me the error output I needed to see:
giblib error: couldn't grab pointer:Resource temporarily unavailable
Which lead me to this post:
https://bbs.archlinux.org/viewtopic.php?id=86507 for the tip.
For whomever the jtlindsey's answer did not work for solving the problem :
giblib error: couldn't grab pointer:Resource temporarily unavailable
Another solution could be this : just before calling scrot, run the command :
xdotool key XF86Ungrab
This should release the pointer, and the scrot command should work after it.
Note : the source claims that before executing previous xdotool command, it would possibly be required to execute :
setxkbmap -option grab:break_actions

Putty executing a text tile in a linux terminal

I have a one liner that I run from the run box in the start menu:
"C:\Program Files (x86)\casper\PuTTY\putty.exe" -ssh "192.168.1.2" -l casper -pw "<password>" -m \\PROD.MSAD.casp.NET\UserData\CASPER\Home\Documents\pbauth_list.txt -t
It sssh to a linux box, and opens up a terminal and echos these statements intoa terminal. when someone needs elevated access at the linux terminal i just type in user=user than cut and paste the line into the command line - and viola that user has access to a super user account for what ever time period i designate. It works awesome.
The
\\PROD.MSAD.casp.NET\UserData\CASPER\Home\Documents\pbauth_list.txt text
file looks like this
echo "pbrun pbauthcl grant PBAUTH_P_A4_PROD \$user 2 \"\$user needs access\" now all
pbrun pbauthcl grant PBAUTH_P_A4_UTIL \$user 2 \"\$user needs access\" now all
printf '\e[8;50;100t'
"
Notice that the
"printf '\e[8;50;100t'"
is echoed out along with the lines into the terminal .
What this command does is automagically logs into a linux box at 192.168.1.2 and then opens up a terminal and prints the lines to the terminal.
i cut and paste the print statement "printf '\e[8;50;100t'" to resize the terminal.
When a user wants elevated access at the linux command prompt
What I do is type in user= and then cut and paste one of the pbun command and it gets executed at the linux command line - which works fine.
however it would be much cooler if the "printf '\e[8;50;100t'" would just execute and make the terminal larger instead of me having to echo it into the terminal and then cut and paste it into the same terminal to resize it.
I have tried a bunch of different permutations in the test file to get the printf statement to just run, instead of echoing it out.
bash -v echo "printf '\e[8;50;100t'"
bash ;
I get thse kinds of errors though
/usr/bin/printf: /usr/bin/printf: cannot execute binary file
/bin/echo: /bin/echo: cannot execute binary file
how do I execute the "printf '\e[8;50;100t'"
command within the terminal instead of echoing it out ? There has to be a way .
Two possibile solutions that might work for you are the following:
Try -x
Try -o verbose

gnome-terminal executes commands from file

I need an example of gnome-terminal command to read lines of text from a file and executes them one by one in different terminal or a tab.
So this would be the process. I would run gnome terminal command and it would read 10 commands from a file. Then it would execute those 10 commands in 10 different tabs/terminals. And of course those tabs/terminals would remain opened. I found this question Avoid gnome-terminal close after script execution?
The third answer from the top is very helpful. I managed to open 1 command from a file. But I need 1 file with 10 command lines to be opened like I wrote above.
Thanks.
I recommend to use screen for this, if that can be acceptable to you.
You could create a commands.screenrc file like this:
screen bash -c 'command1; echo press any key; read'
screen bash -c 'command2; bash'
screen mutt
screen emacs
screen
You can define as many programs as you want. Start screen with:
screen -c commands.screenrc
I don't know what kind of commands you want to run. If you want to see their output, then write like the first example above: execute the command in a bash shell, which will "pause" after the command was executed. Or the second line, which, after running the command will start another bash shell. Otherwise the screen window would exit automatically.
If you are not familiar with screen, you will need to learn some basic key strokes to get around, and to be able to switch between windows. The first few pages of this presentation should be enough to get you started.

How to execute a command inside a screen session

I would like to know how to execute a command inside a screen session.
i searched and I found this :
screen -S nameofscreen -X stuff "command"
When I type this, the command is typed inside the screen but it is not executed.
So my queston is how to press enter using this command.
I'd do something like this:
screen -S sessionName bash -c 'cmd; exec bash'
it starts a new session executes cmd and launches shell (otherwise it'd drop that new session).
-X will allow you to send input to a specified session -- that's why your command didn't execute. To execute it you'd need to add enter sign like Paul suggested. It can be done with Ctrl+v and then Enter. That will produce that ^M. So:
screen -S sessionName -X stuff 'cmd^M'
That, in itself, won't however attach a detached session.
In bash, you can use \n in the $'...' construct:
screen -S nameofscreen -X stuff $'command\n'
In the bash shell you can use ctrl-V to explicitly put non-printable characters into a string. So try ctrl-V ctrl-L ctrl-V ctrl-M at the end of your command just before the ".
It took me some time, but what I found is:
Version of screen 4.06 has a bug.
If you want to send a command over a shared screen session like this, it fails:
screen -S shared_session_name -X stuff "command \n"
Screen fails with an error:
Cannot opendir /run/screen/S-$USER: Permissions denied
After update to the version screen 4.09 it works.

Linux Terminal: typing feedback gone, line breaks not displayed

From time to time I have to run a command-line tool (a Python script) whose output seems to break my terminal.
After the execution is finished, the typing feedback is gone (I can't see what I'm typing), and also line breaks are not displayed. This happens if the terminal is started remotely via Putty, and also locally when using gnome-terminal.
For example, after the problem happens, if I type ENTER pwd ENTER, I would expect to see:
[userA#host006 ~]$
[userA#host006 ~]$ pwd
/home/userA
[userA#host006 ~]$
But actually the output is:
[userA#host006 ~]$ [userA#host006 ~]$ /home/userA
[userA#host006 ~]$
The only way to fix it is to close that terminal and start a new one.
Maybe be related: the script output contains some terminal-based formatting (e.g. invert foreground/background to highlight some status messages). If I dump this output to a file I can see things like [07mSome Message Here[0m.
Any ideas what I could do to prevent this?
Execute the command reset and your terminal should be restored (reference).
This issue happens generally when dumping binary data to the terminal STDOUT which when the escape codes received are processed can do anything from change the color of the text, disable echo, even change character set.
The easy way to avoid this is to ensure you do not dump unknown binary data to the terminal, and if you must then convert it to hexadecimal to ensure it doesn't change the terminal settings.
To elaborate on Joshua Briefman's answer, executing reset -c will only reset the control characters responsible for your problem:
tset, reset - terminal initialization
Usage: tset [options] [terminal]
Options:
-c set control characters
-e ch erase character
-I no initialization strings
-i ch interrupt character
-k ch kill character
-m mapping map identifier to type
-Q do not output control key settings
-r display term on stderr
-s output TERM set command
-V print curses-version
-w set window-size
Also note the following form the command's manual:
Note, you may have to type
<LF>reset<LF>
(the line-feed character is normally control-J) to get the terminal to work, as carriage-return may no longer work in the abnormal state. Also, the terminal will often not echo the command.

Resources