Alter Behavior of xdg-open - linux

I use the command xdg-open quite a lot in my Ubuntu Linux terminal. However, two things irk me:
Is it possible to suppress the error messages?
Is it possible to get the command to always complete? (That is, not continue running, so that I have another "new line" in my terminal).
I realize 2 may not be possible, because of the way the program works, but I imagine 1 is.

First one is easy. Just
alias xdg-open="xdg-open 2>/dev/null"
If you want it permanently, just add that line to ~/.bashrc file.
I recommend you to think twice if you want to become blind to errors, though.
The second one is quite confusing to me. xdg-open shouldn't be intereactive. In my computer (Debian sid) xdg-open execs the command and ends, even if the command itself has not ended (ie: you have not closed the application opened for the URL). I think this should be the behaviour of xdg-open on any platform (it's supposed to work exactly the same way on any XDG system, that's its very purpose).
Anyway, for any command you launch in a shell, if you want it to be non-interactive, that is, to allow to enter commands even if the previous one hasn't finished, you just attach "&" to the end of it. Example:
# prompt is not shown until you close the calculator
$ gnome-calculator
# prompt is shown right after opening calculator and you can
# work on the shell even if you don't close it
$ gnome-calculator &

I maybe late for the answer, but I got exactly the same problem like you have / had.
I tried to start a URL with xdg-open, my default browser is firefox, and not xdg-open but firefox started with an error:
[user#user-pc ~]$ xdg-open https://www.google.de # the page opens fine, but firefox had an error
[user#user-pc ~]$
(process:3783): GLib-CRITICAL **: g_slice_set_config: assertion 'sys_page_size == 0' failed
# needed to press enter here
xdg-open closed fine but the firefox error stayed and I need to press enter to get the bash moving.
To get along this problem I called xdg-open within a new shell putting those output to /dev/null:
bash -c "xdg-open https://www.google.de" 2> /dev/null
The page opened fine, no error shown – rather nothing has been shown. And no need to press enter.

Related

Linux xfce desktop launcher: Run a nested command

With xfce4-terminal (on Manjaro): Running chromium "$(xclip -o)" will open the Chromium web browser and visit the content of the clipboard (which is assumed to be a single URL).
I want a desktop launcher to do exactly this. Creating a new launcher (right click on the desktop > create launcher) with the above command in the command field won't work: Chromium is opened, but the address bar says %24%28xclip%20-o%29.
What is happening? The tiny bit of bash knowledge I have tells me that I need to escape some characters -- which ones? I tried different things like chromium \"$(xclip -o)\" and chromium "\$\(xclip -o\)", which produce similar outcomes, but I can't figure it out.
I found this specification of launcher items, but I seemingly fail to understand it well enough to apply it to my problem. Or am I completely on the wrong track?
Another application would be this: xfce4-screenshooter -f -s "$(date +screenshot_%Y-%m-%d_%T.png)" takes a screenshot and names the resulting file with a time stamp. It works being directly run in the terminal, but not when configured as an application shortcut in the keyboard settings. Analogously, the file is named $(date +screenshot_%Y-%d_%m-%T.png).
Try:
bash -c "chromium $(xclip -o)"
in your command field. That worked for me at least. (Also using Manjaro XFCE)

Linux how to launch visible terminal with script file automatically at startup

I have seen many ways to launch a script like putting it in profile.D, rc.local, or creating a auto start file but none of those launch the file in a visible window if at all. I need it to be in a visible window in Ubuntu. I need to do this because I am using several emulators to stream to different services, and I don't want to have to start the script on each manually.
I am using visual box for the emulator. The sh file is on a removable drive because it is an external file. I also need it to run as sudo.
Edit: I don't actually need it to run at startup. I just need to have the script run. I can probably just sleep really long for graphic to load.
Edit 2: So I created a service that launched a sh file in /usr/bin/ which was supposed to create a gnome-terminal window that ran my script. It ran, however It didn't create a visible window for some reason. I then tried to specify a display which caused gnome to freak out. Dbus was not launching correctly. another question stated that gnome would not work because of how it was designed and stated to use konsole instead. Konsole also stated that it could not connect to a display, giving a QXcbConnection error. Konsole does not have an option to specify display. I don't know what else to try
Edit 3: So I did the thing in the comment. And the service works. However it only works after I run the file that the service runs in usr/bin manually after every restart. The important parts of the file:
#!/bin/bash
sleep 60
ufw disable
ssh nateguana#$(hostname) -X
xhost +
*launch Gnome**only works after file ran manually*
I have also tried exporting DISPLAY, and changing users with su. I have not tried importing SSHD, as another question said to do, as I think that is only for non local connections. I have also tried every single arrangement of commands possible. Xhost errors stating that it is unable to open display "".
You can use gnome-terminal -e <command> to spawn a new bash terminal which runs the command.
You could use something like
gnome-terminal -e /path/to/bashfile
Bear in mind, this will end the terminal after the bash scipt is done executing.
To avoid this,in a newline add $SHELL to the end of your bash script.
PS: the -e argument is deprecated and might be removed in later versions

Linux Terminal PS1

I am not sure if this is possible but I couldn't find the answer anywhere else or even people who have tried it but my current PS1 is this:
export PS1="\[\e[00;32m\]??\[\e[0m\]\[\e[00;37m\]\n\[\e[0m\]\[\e[01;31m\]\d\[\e[0m\]\[\e[00;37m\] \[\e[0m \]\[\e[01;31m\]\T\[\e[0m\]\[\e[00;37m\]\n\[\e[0m\]\[\e[00;32m\]>\[\e[0m\]"
It is showing up and working upon opening the terminal however I am wondering if it was possible to clear the screen after a command finishes (with a prompt similar to windows cmd 'pause' command if possible) so that it isn't repeated or shown at the bottom of the window
I am running Manjaro XFCE if it makes a difference.
Thanks!
You can try something like this:
PS1='$(echo "(press enter to continue)" > /dev/tty ; read ; clear) $ '
NOTE: You need the redirect > /dev/tty because the standard output of the commands run by PS1 goes nowhere.
Replace the ending $ with whatever prompt you want to show.
I don't really think it is such a good idea. In the short time I've been testing that, it is so irritating!!!
I think you are way better just pressing Ctrl+L befor typing your command to clear the terminal.

How can I revert my last choice on terminal if it shows > symbol?

I'll be glad if someone can fix the title to be more appropriate since I'm pretty new to terminal.
I have an issue with terminal. Once I execute a command, if it goes to the next line, I can't close it or revert it. I assume it starts the executable or asks for more parameters using >
For example:
//Windows Machine
vagrant up
//Vagrant Instance Unix Machine
$ git
>
>
>
> ... it goes on like this, I can't close > so I can't execute other commands
The only solution on fixing is restarting the terminal (which means I need to restart Vagrant instance)
It happens on some commands only - not all, so I don't know what makes a difference.
For example, executing composer, I get information about Composer and terminal goes back to main state. However, if I execute things like php, git, mysql, > symbol appears and I can't return from there.
So, two basic questions;
What causes this?
How can I terminate the current command to go back main state?
Any help would be greatly appreciated.
Ps. I use both windows terminal and unix terminal and this issue happens on both.
Normally you'll see a > prompt if you've entered a command that's syntactically incomplete, for example if there's a unterminated string literal:
$ echo 'hello
> '
hello
$
It means that the shell is waiting for you to type the rest of the command, or at least enough of it to make for something that's not a syntax error.
In this example, the default prompt, $PS1, is '$ ', and the secondary prompt, $PS2, is '> '. Read the documentation for your shell (probably bash) for more information.
You can cancel the current command and get back to your primary prompt for a new command by typing Control-C.
This is all about the behavior of your shell; it has nothing to do with your terminal (almost certainly a terminal emulator), which merely provides a GUI for your shell to run in.

Linux: using the tee command via ssh

I have written a Fortran program (let's call it program.exe) with does some simulation for me. Via ssh I'm logging ino some far away computers to start runs there whose results I collect after a few days. To be up-to-date how the program proceeds I want to write the shell output into a text file output.txt also (since I can't be logged in the far away computers all the time). The command should be something like
nohup program.exe | tee output.txt > /dev/null &
This enables me to have a look at output.txt to see the current status even though the program hasn't ended its run yet. The above command works fine on my local machine. I tried first with the command '>' but here the problem was that nothing was written into the text file until the whole program had finish (maybe related to the pipe buffer?). So I used the workaround with 'tee'.
The problem is now that when I log into the computer via ssh (ssh -X user#machine), execute the above command and look at output.txt with the VI editor nothing appears until the program has finished. If I omit the 'nohup' and '&' I will not even get any shell output until it has finished. My thought was that it might have to do something with data being buffered by ssh but I'm rather a Linux newbie. For any ideas or workaround I would be very grateful!
I would use screen utility http://www.oreillynet.com/linux/cmd/cmd.csp?path=s/screen instead of nohup. Thus I would be able to set my program to detached state (^A^D) reconnect to the host, retrieve my screen session (screen -r)
and monitor my output as if I never logged out.

Resources