我确实确实尝试过在问之前找到答案。抱歉,如果这是重复的。这是一项家庭作业。我需要创建一个脚本来提取用户名并在/ etc / password中查找特定信息(用户名,主目录,shell)以显示和写入某些文件。主目录的内容也应显示并写入同一文件。
sudo可以工作,但是我不希望它要求sudo才能工作。
#!/bin/bash
echo "This script will display your username, grab your shell and home directory, provide a list of what is in your home directory, and write this information to a file."
# assign the values to variables
usrEntry=$(grep $USER /etc/passwd)
usrName=$(echo $usrEntry|cut -f1 -d:)
usrHome=$(echo $usrEntry|cut -f6 -d:)
usrShell=$(echo $usrEntry|cut -f7 -d:|cut -f3 -d/))
usrHomeSize=$(ls $usrHome -lh|head -1)
usrHomeList=$(ls $usrHome -lh|tail --lines=+2)
outputFile="$usrHome/UserInfo.txt"
# display username, home directory, and shell
echo "Your username is $usrName"
echo "Your home directory is $usrHome"
echo "You are using the $usrShell shell"
# display contents of home directory
echo "Your home directory contents:"
ls $usrHome -lh
# write results to file, formatted
echo "The script will now append this information to the UserInfo.txt file. If this file does not exist, a new one will be created."
echo "-----New Run-----" >> $outputFile
printf "%s: %s\n" "Username" $usrName "Home Directory" $usrHome "UserShell" $usrShell >> $outputFile
echo "Home Contents" >> $outputFile
printf "%s %s\n" $usrHomeSize >> $outputFile
printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" $usrHomeList >> $outputFile
echo "Thank you for using my script. Maybe someday someone will find this useful"
exit 0
如果不清楚要完成什么,我可以解释更多,但我认为已经涵盖了。我获得了多行权限被拒绝。
/usr/local/bin/UserInfo.sh: line xx: /UserInfo.txt: Permission denied
其中xx是第25至29行。这些都是
printf/echo "some text" >> $outputFile
编辑:从那以后,我发现我实际上无法在自己的主目录中写入或触摸任何内容,因此我必须对其进行修复,并在完成后再次检查该脚本。
编辑2:我的主文件夹由root拥有。修复了仍然无法写入文件的问题
答案 0 :(得分:0)
我认为未设置“ usrHome”。 L15显示什么?
echo "Your home directory is $usrHome"