# Need some shell scripting help

**URL:** <https://forums.speedlife.net/t/need-some-shell-scripting-help/62456>\
**Category:** NYSpeed Off Topic\
**Created:** [February 4, 2009, 8:15am UTC](https://forums.speedlife.net/t/need-some-shell-scripting-help/62456 "2009-02-04T08:15:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![boardjnky4](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@boardjnky4](https://forums.speedlife.net/u/boardjnky4)\
**Post date:** [February 4, 2009, 8:15am UTC](https://forums.speedlife.net/t/need-some-shell-scripting-help/62456/1 "2009-02-04T08:15:34Z")

</div>

I usually script in perl, but I am now needing some sh, and am getting my ass kicked.

I have a file with MANY lines in the following format:

\<servername\> \<username\>

I want to use that information to ssh to the server and remotely execute a command, for example `ssh user@server ls`

The following is what I have:

```auto

#!/bin/sh
       
while read line; do
  line=`echo $line|tr -d "\012"`
  part1=`echo $line | awk '{print $1}'`
  part2=`echo $line | awk '{print $2}'`
  echo "$part1"
  echo "$part2"
  ssh $part2@$part1 ls

  done &lt; $1

```

What happens is that the script executes the ssh command the first time, returns the result, then exits and the whole script dies. It never continues to loop.

If I comment out the ssh and just print out the username and servername, it loops correctly.

What am I missing/doing wrong?

---

<div class="post-metadata">

**Author:** ![boardjnky4](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@boardjnky4](https://forums.speedlife.net/u/boardjnky4)\
**Post date:** [February 4, 2009, 8:53am UTC](https://forums.speedlife.net/t/need-some-shell-scripting-help/62456/2 "2009-02-04T08:53:58Z")

</div>

figured it out, for future reference, ssh needs a -n flag to ignore stdin

since I am passing the $1 into the while loop as stdin, ssh was barfing

---

<div class="post-metadata">

**Author:** ![Matt\_Danger](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/matt_danger/32/4981_2.png) [@Matt\_Danger](https://forums.speedlife.net/u/Matt_Danger)\
**Post date:** [February 4, 2009, 8:54am UTC](https://forums.speedlife.net/t/need-some-shell-scripting-help/62456/3 "2009-02-04T08:54:06Z")

</div>

Not sure. Looks fine and works for me and I can’t think of any characters that SSH would return that would break a loop, but I could be missing something. Are you invoking the script from a normal user or through some other process?

Edit: good catch.

---

<div class="post-metadata">

**Author:** ![LZ1](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@LZ1](https://forums.speedlife.net/u/LZ1)\
**Post date:** [February 4, 2009, 9:51am UTC](https://forums.speedlife.net/t/need-some-shell-scripting-help/62456/4 "2009-02-04T09:51:46Z")

</div>

> **[Distributed administration using SSH](https://www.ibm.com/developerworks/aix/library/au-satdistadmin/index.html)**
>
> Use Secure Shell (SSH) to run commands on remote UNIX(R) systems and, with some simple scripts, put together a system that enables you to manage many systems simultaneously from one machine without having to log in directly to the machines...

---

<div class="post-metadata">

**Author:** ![LZ1](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@LZ1](https://forums.speedlife.net/u/LZ1)\
**Post date:** [February 4, 2009, 9:54am UTC](https://forums.speedlife.net/t/need-some-shell-scripting-help/62456/5 "2009-02-04T09:54:43Z")

</div>

I guess you could debug it by using echo before the ssh command

---

<div class="post-metadata">

**Author:** ![boardjnky4](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@boardjnky4](https://forums.speedlife.net/u/boardjnky4)\
**Post date:** [February 4, 2009, 9:55am UTC](https://forums.speedlife.net/t/need-some-shell-scripting-help/62456/6 "2009-02-04T09:55:22Z")

</div>

already figured it out, see post #2. Thanks for the links though.
