I have a public/private key pair set up so I can ssh to a remote server without having to log in. I'm trying to write a shell script that will list all the folders in a particular directory on the remote server. My question is: how do I specify the remote location? Here's what I've got:
#!/bin/ba开发者_开发问答sh
for file in myname@example.com:dir/*
do
if [ -d "$file" ]
then
echo $file;
fi
done
Try this:
for file in `ssh myname@example.com 'ls -d dir/*/'`
do
echo $file;
done
Or simply:
ssh myname@example.com 'ls -d dir/*/'
Explanation:
- The ssh command accepts an optional command after the hostname and, if a command is provided, it executes that command on login instead of the login shell; ssh then simply passes on the stdout from the command as its own stdout. Here we are simply passing the ls command.
- ls -d dir/*/ is a trick to make ls skip regular files and list out only the directories.
精彩评论