-1
我有一個名爲AddUsers.sh的bash腳本。該腳本應該採取這樣一行:Linux命令不能在bash中工作
[email protected];1927/09/04;sudo,visitor;/visitorData
,並返回變量是這樣的:
name=john
surname=mccarthy
bdate=1927/09/04
uname=mccjoh
pass=1927
groups(array)=sudo, visitor
folder=/visitorData
當我運行該腳本,並給它所需的文本文件,它是tellilng我說,「GROUPADD」 'chgrp''chmod'和'chage'都是錯誤。有人能夠告訴我爲什麼/給我任何反饋?
在此先感謝您的幫助。
#!/bin/bash
#check for file
while [ ! -f $file ]
do
#ask user for filename
echo What is the filename?
#reading input as file name
read file
if [ ! -f $file ]
then
echo "File not found!"
else
echo "File found!"
fi
done
#process each line and make a user from the data
cat "$file" | while read line
do
name=`echo $line | sed 's/^\(.*\)\..*\@.*$/\1/g'`
surname=`echo $line | sed 's/^.*\.\(.*\)\@.*$/\1/g'`
bdate=`echo $line | sed 's/^.*;\(.*\);.*;.*$/\1/g'`
#set groups to tokenize
groups=`echo $line`
folder=`echo $line`
temp2=`echo $name | sed 's/^\(...\).*$/\1/g'`
temp1=`echo $surname | sed 's/^\(...\).*$/\1/g'`
user="${temp1}${temp2}"
#pass must be first 4 numbers of birthdate
pass= ${bdate:0:4}
#tokenise group + add to array
declare -a groupArray
IFS=" "
groupArray=(`echo $groups | tr "," " "`)
#create groups if not existing.
for i in ${groupArray[@]}
do
if [ getent group $i ]
then
echo "group exists"
else
groupadd $i
fi
done
#Create shared folders if not existing.
if [ ! -d $folder ];
then
mkdir -p $folder
fi
#Create groups for shared folders
gname=`echo "${folder:1}"`
groupadd $gname
#Set group as owner of directory and change permissions
chgrp -R $gname $folder
chmod -R 770 $folder
#create user and add to groups
if [ grep "^${user}:" /etc/passwd ]
then
echo "user exists already!"
else
#Create user
useradd -m -d /home/$user -p $pass $user
#Add user to groups
for i in ${groupArray[@]}
do
usermod -a -G $i $user
done
#Add user to shared group
usermod -a -G $gname $user
fi
#force password change
chage -d 0 $user
done
通過http://shellcheck.net –
也跑你的腳本,而不是整個腳本,將其縮小到發生錯誤的位置會有所幫助。請參閱http://stackoverflow.com/help/mcve –
,在希望腳本吐出其狀態的位置添加'set + x',並在希望停止的位置放置'set -x'。通常情況下,如果你不想監視整個腳本,只需在文件的頂部放置'set + x',在##/ bin/bash後面# – jiveturkey