Hi Nicholas,

From: "Nicholas Marriott" <nicholas.marri...@gmail.com>
> On Sun, May 29, 2011 at 12:28:04AM +0200, Helmut Schneider wrote:
>> Imagine a shell script which takes command line options (e.g. using 
>> getopts)
>> and pass that script to tmux:
>
> Please give me an example of exactly what you are trying to do that is
> impossible.

I have a script, more a wrapper, that checks for screen and sudo and if 
neither screen or sudo are active the script is restarted using screen 
and/or sudo:

[helmut@BSDHelmut ~]$ cat ./check_for_root_and_start_screen.sh
#!/bin/sh
set -x
PATH=/bin:/usr/bin:/usr/local/bin
if [ -t 0 -a -x "$(which screen)" -a ! "${TERM}" = "screen" ]; then
        $(which screen) "${@}"
        exit 1
fi
#if [ -t 0 -a -x "$(which tmux)" -a ! "${TERM}" = "screen" ]; then
#        $(which tmux) new "${@}"
#        exit 1
#fi
exit 0
[helmut@BSDHelmut ~]$

I have another script that patches/updates hosts. Here, it only displays 
those hosts:

[helmut@BSDHelmut ~]$ cat ./patch_all.sh
#!/bin/sh
#/home/helmut/check_for_root_and_start_screen.sh $0 "${@}" || exit $? # for 
tmux
/home/helmut/check_for_root_and_start_screen.sh $0 "${@}" || exit $? # for 
screen

while getopts "H:" option
do
  case ${option} in
    H)    HOSTS="${OPTARG}"
          ;;
  esac
done
shift `expr ${OPTIND} - 1`
for HOST in $(echo ${HOSTS}); do
        echo ${HOST}
done
[helmut@BSDHelmut ~]$

This works fine with screen:

[helmut@BSDHelmut ~]$ ./patch_all.sh -H "host1 host2 host3"
+ PATH=/bin:/usr/bin:/usr/local/bin
+ which screen
+ [ -t 0 -a -x /usr/local/bin/screen -a ! xterm = screen ]
+ which screen
+ /usr/local/bin/screen -q ./patch_all.sh -H 'host1 host2 host3'
+ PATH=/bin:/usr/bin:/usr/local/bin
+ which screen
+ [ -t 0 -a -x /usr/local/bin/screen -a ! screen = screen ]
+ exit 0
host1
host2
host3
[screen is terminating]
+ exit 1
[helmut@BSDHelmut ~]$

I now tried to adapt that to use tmux so I uncomment/comment the 
corresponding lines above:

[helmut@BSDHelmut ~]$ ./patch_all.sh -H "host1 host2 host3"
+ PATH=/bin:/usr/bin:/usr/local/bin
+ which tmux
+ [ -t 0 -a -x /usr/local/bin/tmux -a ! xterm = screen ]
+ which tmux
+ /usr/local/bin/tmux new ./patch_all.sh -H 'host1 host2 host3'
usage: new-session [-d] [-n window-name] [-s session-name] [-t 
target-session] [command]
+ exit 1
[helmut@BSDHelmut ~]$

If it is of interest I then put an alias into /etc/profile:

patch_all_helmut_func () {
        /usr/local/bin/patch_all.sh -puncH "host1 host2 host3" "${@}"
}

(The last "${@}" is used to provide additional arguments, the "real" 
patch_all.sh script is a bit longer. It is mandatory that the script runs 
with Bourne shell to be portable with other systems)

Helmut 


------------------------------------------------------------------------------
vRanger cuts backup time in half-while increasing security.
With the market-leading solution for virtual backup and recovery, 
you get blazing-fast, flexible, and affordable data protection.
Download your free trial now. 
http://p.sf.net/sfu/quest-d2dcopy1
_______________________________________________
tmux-users mailing list
tmux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tmux-users

Reply via email to