我有一个想法,如何检查主目录中每个用户的“id_rsa.pub”的可用性。但有些东西不起作用。我的脚本:
#!/bin/bash
users=`ls /home/`
for i in "$users"; do
if [[ -f "/home/$i/.ssh/id_rsa.pub" ]]; then
echo "All users have this key"
else
echo "Users $i don't have this key, We need build key for this users"
fi
done
在调试中:
+ [[ -f /home/donovan
valeri
john
roman
colbi
testuser/.ssh/id_rsa.pub ]]
在我看来,它需要完整路径,但不是每个用户的完整路径。请帮忙,我做错了什么?谢谢你的关注。当然,我得到了一个结果:
Users donovan
valeri
john
roman
colbi
testuser don't have this key, We need build key for this users
答案1
引号导致所有用户名被视为单个字符串:
for i in "$users"; do
删除引号就可以了:
for i in $users; do
用户名不包含空格,因此您在这里应该是安全的。
答案2
ls /home
在脚本中使用不是一个好主意。是ls
一个人类可读的列表命令,并不适合在脚本中使用。
您可以简单地创建一个数组并循环遍历索引,例如
#!/usr/bin/env bash
users=(/home/*)
for i in "${users[@]}"; do
if [[ -f "$i/.ssh/id_rsa.pub" ]]; then
echo "All users have this key"
else
echo "Users $i don't have this key, We need build key for this users"
fi
done