# a couple of scripts for screen users



## rbelk (Feb 10, 2009)

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 (Feb 10, 2009)

Nitpicky: you shouldn't create scripts beginning with #!/bin/bash (use #!/bin/sh instead) and you should return non-zero on error.


----------



## rbelk (Feb 10, 2009)

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@ (Feb 10, 2009)

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 (Feb 10, 2009)

Suggestions aside, it is a great post, nice tip!


----------



## rbelk (Feb 10, 2009)

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


----------



## lme@ (Feb 11, 2009)

rbelk said:
			
		

> 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 (Feb 11, 2009)

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 (Feb 11, 2009)

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


----------



## rbelk (Feb 11, 2009)

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 (Feb 11, 2009)

rbelk said:
			
		

> 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 (Feb 11, 2009)

rbelk said:
			
		

> 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.
> ...



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


----------



## mjguzik (Feb 11, 2009)

vivek said:
			
		

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


----------



## rbelk (Feb 11, 2009)

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.


----------

