PDA

View Full Version : a couple of scripts for screen users


rbelk
February 10th, 2009, 03:21
I am an avid user of the utility screen, it's my favorite window manager! Below is a couple of shell scripts for screen users.

- ns <hostname> - will start a new screen and ssh to the <hostname> provided.
- na <application> - will start a new screen and start the <application> provided.


#!/bin/bash

if [ $# = 1 ]; then
# echo "number of arguments is $#"
screen -t $1 ssh -o ServerAliveInterval=300 -o TCPKeepAlive=yes $1
else
echo "USAGE: `basename $0` <hostname>"
echo "This will start a ssh session to a server in a new screen"
fi


#!/bin/bash

if [ $# = 1 ]; then
# echo "number of arguments is $#"
screen -t $1 $1
else
echo "USAGE: `basename $0` <hostname>"
echo "This will start an application on a new screen"
fi

mjguzik
February 10th, 2009, 04:06
Nitpicky: you shouldn't create scripts beginning with #!/bin/bash (use #!/bin/sh instead) and you should return non-zero on error.

rbelk
February 10th, 2009, 04:24
I'll correct it next time. I have to abmin Linux and FreeBSD machines at work and it's easier to just to simlink /bin/bash to where ever bash is. I should use "env" instead of simlinking, my bad.

lme@
February 10th, 2009, 16:34
Actually you can use #/bin/sh on _every_ Unix-System. You just need to make sure to not use any bashisms, so that your script can be portable.

r-c-e
February 10th, 2009, 20:41
Suggestions aside, it is a great post, nice tip!

rbelk
February 11th, 2009, 00:47
lme@, I wish there was a utility that would tell you if you were using bashism's.:e

lme@
February 11th, 2009, 11:59
lme@, I wish there was a utility that would tell you if you were using bashism's.:e

Like this? ;-)

grep '#!/bin/bash' $file && echo "AAAAAAAAAaaaaarrrrrrrrggggghhhhhh"

vivek
February 11th, 2009, 14:53
Following should work under any UNIX like system to show your current shell:
ps -p $$

Edit: I did not read above post, this command is about finding out your current shell ...

rbelk
February 11th, 2009, 15:18
Let me rephrase my comment, I wish there was a utility that scans shell scripts for bashism's.

rbelk
February 11th, 2009, 15:21
Vivek, an easier way to find the type of shell you are running is using shell=`echo $0` in the shell script, I have acually used that before.

vivek
February 11th, 2009, 20:04
Vivek, an easier way to find the type of shell you are running is using shell=`echo $0` in the shell script, I have acually used that before.

nice, I wasn't aware of this tiny hack!

Let me rephrase my comment, I wish there was a utility that scans shell scripts for bashism's.

May be you can use something as follows:
#!/bin/sh
DIR=$@
BASHPATH=/bin/bash
[ $# -eq 0 ] && ( echo "Usage: $0 dir-nanme"; exit 1)
for d in $DIR
do
FILES=$(find $d -type f -print)
for f in $FILES
do
grep "#!${BASHPATH}" $f > /dev/null 2>&1 && echo $f
done
done

estrabd
February 11th, 2009, 20:43
I am an avid user of the utility screen, it's my favorite window manager! Below is a couple of shell scripts for screen users.

- ns <hostname> - will start a new screen and ssh to the <hostname> provided.
- na <application> - will start a new screen and start the <application> provided.


#!/bin/bash

if [ $# = 1 ]; then
# echo "number of arguments is $#"
screen -t $1 ssh -o ServerAliveInterval=300 -o TCPKeepAlive=yes $1
else
echo "USAGE: `basename $0` <hostname>"
echo "This will start a ssh session to a server in a new screen"
fi


#!/bin/bash

if [ $# = 1 ]; then
# echo "number of arguments is $#"
screen -t $1 $1
else
echo "USAGE: `basename $0` <hostname>"
echo "This will start an application on a new screen"
fi


I am confused - is this to be invoked within screen or outside of it?

mjguzik
February 11th, 2009, 21:31
nice, I wasn't aware of this tiny hack!



May be you can use something as follows:
[CODE]#!/bin/sh
DIR=$@
BASHPATH=/bin/bash
[..]

"#!/bin/bash" at the beginning of the script is not the only possible bashism, so I believe this is still not what rbelk wanted.

/bin/sh will tell you that the script uses bashisms by complaining because of syntax errors or not interpreting the script as expected. ;) Just use #!/bin/sh from now on.

rbelk
February 11th, 2009, 23:20
Estrabd, these scripts are to be used only from inside screen. I start my screen session on my local box and the use "na elinks" to start elinks on a new screen. Also "ns system1" will start a ssh session to system1 on a new screen.

Do not use these from the regular command line, it will confuse you. I will add some error checking to make sure you are in a screen session before running them.