-1
나는 AddUsers.sh라는 bash 스크립트를 가지고 있습니다. 이 같은 변수를리눅스 명령이 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
을 내가 스크립트를 실행하고 그것에게 필요한 텍스트 파일을 제공 할 때, 'groupadd'내게 tellilng한다 '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 –
을 통해 스크립트를 실행 chmod를하는 $ 전체 스크립트를 사용하면 오류가 발생한 위치를 좁히는 것이 좋습니다. http://stackoverflow.com/help/mcve –
스크립트가 상태를 빠져 나가기를 원하는 지점에'set + x'를 추가하고 멈추고 싶은 곳의'set -x'를 추가하면됩니다. 일반적으로 전체 스크립트를 감시하는 것을 원하지 않는다면,'#!/bin/bash' 다음에'set + x'를 파일의 맨 위에 놓으십시오. – jiveturkey