My code .sh cant run. Help me

#!/usr/bin/expect -f set port [lindex $argv 0] spawn ps xu | grep "ssh -f -N -o ConnectTimeout=5 -D $port" | grep -v grep | awk '{ print $2 }' | xargs kill -9 expect eol 

My code .sh cant run. Help me

I make sure 100% port 1234 is running. Because i check it ps aux|grep ssh . and see. So what wrong in my code ? Thank


When you put this in a shell script, the $2 will be replaced by argument number 2:

                  v This
./ one two

When you ran your script, you did not include a second argument.

However, this is besides the point, because you want, specifically:

awk '{ print $2 }'

You need to escape the '$' symbol with '\'.

This should work:

spawn ps xu | grep "ssh -f -N -o ConnectTimeout=5 -D $1" | grep -v grep | awk '{ print \$2 }' | xargs kill -9

The first line wasn't necessary, since the port will be the first argument you can use '$1' to get it.

this look like a complex way to issue a kill, if expect is not mandatory

ps xu |awk -v port=$1 'index($0,"ssh -f -N -o ConnectTimeout=5 -D " port) > 0 { printf "kill %d\n",$2}' | bash


  • -v port=$1 initialive awk variable with $1
  • index($0,"...") will search the current output of ps for ssh
Category: linux Time: 2016-07-29 Views: 0
Tags: linux bash ssh grep

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.144 (s). 12 q(s)