嘿,我正在运行与文件夹和文件交互的 shell 脚本,但我的 $PWD 一直指向 /home/MyUserName 目录,这会干扰它应该执行的操作。我正在运行 Linux 4.4.8-300.fc23.x86_64 GNU/Linux:
#!/usr/bin/bash -x
clear
ls $PWD
#Check Root
echo checking Root user
if [ $EUID -ne 0 ]
then
sudo su
fi;
#Check internet availablity
TEST=$(ping -c 1 74.125.21.14|wc -l)
if [ $TEST -gt 5 2>&1 ]
then
sudo dnf install gcc kernel-devel binutils cpp glibc-devel glibc-headers isl kernel kernel-headers libmpc
else
echo "No Internet Connection Available! Installing RPMs manuely."
arr=(*.rpm)
for (( i=0; i<${#arr[@]}; i++ ));
do
echo " ${arr[$i]} "
dnf -y install ${arr[$i]}
done
fi;
# Get Network Ability
echo "Starting the setup of your laptop's wireless card."
if [ -d ./MyWirelessCard ]
then
FILE=./MyWirelessCard/fedora23_broadcom_wl_install.sh
uname1=" $( stat -c "%u" $FILE ) "
[ " $USERNAME1 = 1000" ] || chown 1000 $FILE
[ " -r -w -x $FILE " ] || chmod +rwx $FILE
else
mkdir ./MyWirelessCard
FILE=./MyWirelessCard/fedora23_broadcom_wl_install.sh
uname1=" $( stat -c "%u" $FILE ) "
[ " $USERNAME1 = 1000" ] || chown 1000 $FILE
[ " -r -w -x $FILE " ] || chmod +rwx $FILE
fi;
#Making wireless card installer run on boot
#echo "Making the wireless card install script run on boot"
#ln -s ./MyWirelessCard/fedora23_broadcom_wl_update.sh /etc/init.d/
#ln -s ./MyWirelessCard/fedora23_broadcom_wl_update.sh /etc/rc.d/
# Initial Update
echo "Preforming initial system update."
dnf -y update
#Exit
echo Script is finished
exit
我得到:
- ls /home/MyUserName 文件夹内容
- echo 检查 Root 用户 检查 Root 用户
- '[' 1000 -ne 0 ']'
- sudo su [sudo] MyUserName 的密码: ++ ping -c 1 74.125.21.14 ++ wc -l
- 测试=6
- '['6-gt5']'
- sudo dnf install gcc kernel-devel binutils cpp glibc-devel glibc-headers isl kernel kernel-headers libmpc [sudo] dcarr 的密码:
- echo '开始设置您的笔记本电脑的无线网卡。' 开始设置您的笔记本电脑的无线网卡。
- '['-d./MyWirelessCard']'
- mkdir ./MyWirelessCard
- FILE=./MyWirelessCard/fedora23_broadcom_wl_install.sh ++ stat -c %u ./MyWirelessCard/fedora23_broadcom_wl_install.sh stat: 无法统计‘./MyWirelessCard/fedora23_broadcom_wl_install.sh’: 没有此文件或目录
- uname1=' '
- '[' ' = 1000' ']'
- '[' ' -r -w -x ./MyWirelessCard/fedora23_broadcom_wl_install.sh ' ']'
- echo '正在执行初始系统更新。' 正在执行初始系统更新。
- dnf -y update 错误:此命令必须在 root 用户下运行。
- echo 脚本已完成 脚本已完成
- 出口
我很茫然,如能得到任何帮助我将非常感激。
答案1
您必须实际更改脚本中的目录。通常:
cd "$(dirname "$0")"