Plan 9 from Bell Labs’s /usr/web/sources/contrib/yk/lab/unix/r

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#!/Users/yarek/plan9/bin/rc
#
# Executes a command over SSH, taking care of the following, along the way:
# - the remote host name is your environment, $CPU, or deduced with dircpu
# - the remote working directory is deduced from local one, withing certain convention
# - the remote command line is escaped to preserve argument lists as is
# - the remote environment is initialized from your profiles
#
# To publish: cp $% /n/sources/contrib/yk/lab/unix/
#

. /Users/yarek/plan9/bin/9.rc

fn quotesh {
	whatis '*' | sed 's!\*=!!; s!^[(]!!; s![)]$!!; s!''''!''\\''''!g'
}


passflags = ()
if(~ $1 -*) {
	passflags = $1
	shift
}

if(~ $#CPU 0)
	CPU = `{dircpu}
if(! ~ $#CPU 1){
	echo >[2=1] 'r: pick one CPU'
	exit usage
}

if(! ~ $#% 0){	# sam, acme
	if(~ $"% */)	# acme
		dir = $"%
	if not
		dir = `{dirname $"%}
}
if not
	dir = `{pwd}

remdir = `{echo $"dir | sed '
	s#(^' ^ $HOME ^ '|^)/(n)/(remote|local|other|'$CPU^')($|/)#/#
	s#^'$HOME^'$#.#
'}

pre = ()
if(~ $#rnoprofile 0)
	pre = ($pre '. /etc/profile; . ./.profile;')
if(~ $#rnocwd 0)
	pre = ($pre '{ \cd '$remdir^'; } >&2;')

if(~ $#* 0){
	passflags = (-t)
	* = (bash -li)
}

nl  = '
'
argv = ()
# rewrite remote absolute pathnames in the arguments
for(arg) {
	if(~ $arg */n/^(remote local other $CPU)^/* && test -e $arg) {
		IFS=$nl {
			arg = `{echo $arg | sed 's!(^' ^ $HOME ^ '|^)/(n)/[^/]*/!/!'}
		}
	}
	argv = ($argv $arg)
}
ifs='' {
	* =  ($pre `{quotesh $argv})
}
exec ssh $passflags $CPU $*

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].