Basically i need to have a script that checks if there is a screen running as "serverman".
If it exists, then resume it, otherwise create new.
So i wanted to parse "sceen -ls" and check if there is one called "serverman".
This is "screen -ls" output
There are screens on:
7423.212 (03/09/14 08:48:58) (Detached)
7411.1as (03/09/14 08:48:49) (Detached)
2 Sockets in /var/run/screen/S-root.
I was tinking to a command that could parse it trough piping and search for "serverman" word.
And if any set isalreadyrunning=1, else set it to 0, but i don't know what command to use and how to use, do you?
You can already do this with existing screen options without explicitly checking for a named session. screen -D -R serverman will attach to serverman if exists, but will create it and attach to it if not.
I paste a working script for tmux, as apparently I have old screen on my old MacBook that gives different output. You should be able to modify it easily (but I would advice to switch to tmux, it's way better)
# created sessions with
# tmux new -s hello
# tmux new -s world
SEARCHING="hello"
found_session=$(tmux ls 2> /dev/null | perl -ne "if (/(^${SEARCHING}):/) {print \"\$1\"}" | head -n1)
if [ -z "$found_session" ]; then
echo Session not found, creating new.
tmux new -s $SEARCHING
else
echo Found $found_session, attaching.
tmux a -t $found_session
fi
Related
I compiled mplayer from source on Ubuntu. I didn't want to use a GUI but I wanted to make a executable bash file that gets the path from an file that gets dropped onto the bash file. How do I make such a thing possible?
I wanted to make it look something like this:
mplayer <get full path to file.file-ending>
I want the executable bash file to sit on my desktop ;)
If possible, I'd just like an rightclick -> start with mplayer function, but I don't know how to make one.
You can access arguments passed to the script with $1 (for the first argument). And also you should make a .desktop file so Nautilus (or your desktop manager) know what to do and use %u to pass the dropped path to the script.
For example you can create a file named DropOverMe.desktop:
[Desktop Entry]
Encoding=UTF-8
Name=Drop Over Me
Comment=Execute the script with the file dropped
Exec=gnome-terminal -e "/folder/to/the/script/launchme.sh \"%u\""
Icon=utilities-terminal
Type=Application
I use gnome-terminal as I have Ubuntu on my PC, use your preferred terminal application.
And the script could be something like:
#! /bin/bash
echo "Launched with $1" >> /tmp/history.log
Try:
#!/bin/bash
mplayer "$1"
The file path of the dropped file will be passed to the script file as the 1th command line argument.
In openned terminal
By using mate-terminal, gnome-terminal or konsole, you could use drag'n drop into oppened window.
This will send URL as tipped, with a space added, but without endline.
For this, run mplayer, I wrote this little loop function:
while IFS=$' \t\r\n' read -d '' -p "Wait for URL to play: " -rsn 1 str &&
[ "$str" ];do
while IFS= read -d '' -rsn 1 -t .02 char
do str+="$char"
done
if [ "$str" ] ;then
read -a req <<<"$str"
echo $req
mplayer $req
fi
done
First read will determine if something is comming or else end loop.
Second loop with very short timeout will read dropped URL as a single string
read -a req will split string to consider only 1st part
When writing shell scripts, is the an idiom or swift way to run a program only if it is installed, and if it is not, just let it be (or handle the error in some other way apart from installing it)?
More specifically, I have a lot of servers which I access over ssh, and whenever I get a new server, I simply copy all my rc-files to it. The .zshrc starts tmux unless it is already running. Some of the servers (not all) do not have tmux installed. I do not want to install it because of disk space limitations, I do not want to have different rc-files for different servers, and I do not want my rc-files to be interrupted when executing them.
I have seen solutions involving apt-cache policy <package-name>, so I guess I could use that and pipe it to something like grep -e 'Installed: (none)', but that would assume that the server is running Debian or Ubuntu, which I can not do, and it would only work for packages that were installed with apt, not things I have installed in other ways.
command -v <command> is the common (and POSIX) way to check if a command could be executed (is executable and on the $PATH).
E.g:
command -v tmux >/dev/null &&
tmux a -t name
(>/dev/null since, if the command exists, its path will be printed to STDOUT.)
It could be nice to put it in a reusable function:
maybe() {
! command -v "${1}" >/dev/null ||
"$#"
}
Then one could use:
maybe tmux a -t name
And if tmux is available then tmux a -t name will be run, otherwise it’ll be silently ignored.
Or, if you want some feedback when a command is not available:
maybe() {
if command -v "${1}" >/dev/null
then
"$#"
else
printf 'Command "%s" not available, skipping\n' "${1}" >&2
fi
}
This might help-
1) Assuming tmux is available in PATH (as it must be executable)
isAvailable=$(type -P tmux)
if [[ -x $isAvailable ]]; then
...
2) Verify file is present on a specific path (Copying all rc-files)
export FILEPATH="..."
if[[ -f $FILEPATH ]]; then
I am trying to write a script to change between speakers and headset. I want to be able to change the output and the input with a shortcut.
I used these commands to see my audio devices :
$ pacmd list-sinks | grep alsa_output
name: <alsa_output.usb-Logitech_Logitech_USB_Headset-00.analog-stereo>
name: <alsa_output.pci-0000_00_1b.0.analog-stereo>
$ pacmd list-sources | grep alsa_input
name: <alsa_input.usb-Logitech_Logitech_USB_Headset-00.analog-mono>
name: <alsa_input.usb-093a_262c-01.analog-mono>
name: <alsa_input.pci-0000_00_1b.0.analog-stereo>
And wrote this script so I can change the default audio device.
#! /bin/bash
pacmd set-default-sink alsa_output.usb-Logitech_Logitech_USB_Headset-00.analog-stereo
pacmd set-default-source alsa_input.usb-Logitech_Logitech_USB_Headset-00.analog-mono
exit 0
When I run this with the command
sudo ./usb-speakers.sh
I get this messages :
No PulseAudio daemon running, or not running as session daemon.
Home directory not accessible: Permission denied
No PulseAudio daemon running, or not running as session daemon.
I found several questions with similar problems but I can't make anything work for me.
https://github.com/mk-fg/pulseaudio-mixer-cli/blob/master/README.md says:
Kinda like alsamixer, focused not on sink volume levels (which can actually be controlled via alsamixer, with alsa-pulse plugin), but rather on volume of individual streams, so you can turn down the music to hear the stuff from games, mumble, skype or browser.
In addition to interactive UI, script allows to match and configure sink/stream parameters via config file, so that when specific sink or stream appears, e.g. its volume can be capped, port changed, UI title adjusted, be hidden in UI, stuff like that.
I found this script (I can't remember where ) which solve all my problems
#!/bin/bash
declare -i sinks=(`pacmd list-sinks | sed -n -e 's/\**[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`)
declare -i sinks_count=${#sinks[*]}
declare -i active_sink_index=`pacmd list-sinks | sed -n -e 's/\*[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`
declare -i next_sink_index=${sinks[0]}
#find the next sink (not always the next index number)
declare -i ord=0
while [ $ord -lt $sinks_count ];
do
echo ${sinks[$ord]}
if [ ${sinks[$ord]} -gt $active_sink_index ] ; then
next_sink_index=${sinks[$ord]}
break
fi
let ord++
done
#change the default sink
pacmd "set-default-sink ${next_sink_index}"
#move all inputs to the new sink
for app in $(pacmd list-sink-inputs | sed -n -e 's/index:[[:space:]]\([[:digit:]]\)/\1/p');
do
pacmd "move-sink-input $app $next_sink_index"
done
#display notification
declare -i ndx=0
pacmd list-sinks | sed -n -e 's/device.description[[:space:]]=[[:space:]]"\(.*\)"/\1/p' | while read line;
do
if [ $(( $ord % $sinks_count )) -eq $ndx ] ; then
notify-send -i notification-audio-volume-high --hint=string:x-canonical-private-synchronous: "Sound output switched to" "$line"
exit
fi
let ndx++
done;
just copy and paste it in an empty document and save it as
audio-device-swithcer.sh
then save it to your usr/local/bin directory
If you want to add a shortcut go to Keyboard & Shortcuts and add a new shortcut.
As a command just put your audio-device-switcher.sh file
That's all guys that worked perfectly for me .
And thank you for your answers.
As shown in this answer, it's possible to use read with Readline (-e) in bash to return previous history items by using the up and down keys:
#! /usr/bin/env bash
while IFS="" read -p "input> " -e line; do
history -s "$line" # append $line to local history
done
What is the right way to do this in zsh? (getting user input on a loop and allowing for up/down key history completion). This doesn't work:
#! /usr/bin/env zsh
while IFS="" vared -p "input> " -c line; do
done
I think history completion is disabled by default on scripts in zsh. Also, I don't want the history to come from the shell, but from the input that is entered in the script.
I think you're asking for something along these lines... untested
#! /bin/zsh -i
local HISTFILE
# -p push history list into a stack, and create a new list
# -a automatically pop the history list when exiting this scope...
HISTFILE=$HOME/.someOtherZshHistoryFile
fc -ap # read 'man zshbuiltins' entry for 'fc'
while IFS="" vared -p "input> " -c line; do
print -S $line # places $line (split by spaces) into the history list...
done
[EDIT]
Notice I added -i to the first line (#!). It is merely a way to indicate that the shell must be running in interactive mode. The best way to achieve this is to
simply execute the script with zsh -i my-script.zsh, because passing arguments to #! commands differs between Linux and OSX, so it is in principle something one should not rely on.
Honestly, why don't you just start a new interactive shell using some custom configuration and (should it be necessary) hooks between commands? The best way to achieve this is likely to just start a new shell using different config files a new history.
This is a much better way to do this:
mkdir ~/abc
echo "export HISTFILE=$HOME/.someOtherZshHistoryFile;autoload -U compinit; compinit" >! ~/abc/.zshrc
ZDOTDIR=~/abc/ zsh -i
you can then change the script's config file to perform any other customisation you need (different color prompt, no history saving etc).
To actually do things with the user input, you should use one of the many hooks handled by add-zsh-hook
I compiled mplayer from source on Ubuntu. I didn't want to use a GUI but I wanted to make a executable bash file that gets the path from an file that gets dropped onto the bash file. How do I make such a thing possible?
I wanted to make it look something like this:
mplayer <get full path to file.file-ending>
I want the executable bash file to sit on my desktop ;)
If possible, I'd just like an rightclick -> start with mplayer function, but I don't know how to make one.
You can access arguments passed to the script with $1 (for the first argument). And also you should make a .desktop file so Nautilus (or your desktop manager) know what to do and use %u to pass the dropped path to the script.
For example you can create a file named DropOverMe.desktop:
[Desktop Entry]
Encoding=UTF-8
Name=Drop Over Me
Comment=Execute the script with the file dropped
Exec=gnome-terminal -e "/folder/to/the/script/launchme.sh \"%u\""
Icon=utilities-terminal
Type=Application
I use gnome-terminal as I have Ubuntu on my PC, use your preferred terminal application.
And the script could be something like:
#! /bin/bash
echo "Launched with $1" >> /tmp/history.log
Try:
#!/bin/bash
mplayer "$1"
The file path of the dropped file will be passed to the script file as the 1th command line argument.
In openned terminal
By using mate-terminal, gnome-terminal or konsole, you could use drag'n drop into oppened window.
This will send URL as tipped, with a space added, but without endline.
For this, run mplayer, I wrote this little loop function:
while IFS=$' \t\r\n' read -d '' -p "Wait for URL to play: " -rsn 1 str &&
[ "$str" ];do
while IFS= read -d '' -rsn 1 -t .02 char
do str+="$char"
done
if [ "$str" ] ;then
read -a req <<<"$str"
echo $req
mplayer $req
fi
done
First read will determine if something is comming or else end loop.
Second loop with very short timeout will read dropped URL as a single string
read -a req will split string to consider only 1st part