Alterando meu script bash do shell padrão $ PWD para o local atual do script (do script)

1

Ei, estou executando meu shell script que interage com pastas e arquivos, mas meu $ PWD continua apontando para o diretório / home / MyUserName e isso mexe com o que é suposto fazer. Estou executando o 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

Eu recebo:

  • ls /home/MyUserName Contents Of Folder
  • echo checking Root user checking Root user
  • '[' 1000 -ne 0 ']'
  • sudo su [sudo] password for MyUserName: ++ ping -c 1 74.125.21.14 ++ wc -l
  • TEST=6
  • '[' 6 -gt 5 ']'
  • sudo dnf install gcc kernel-devel binutils cpp glibc-devel glibc-headers isl kernel kernel-headers libmpc [sudo] password for dcarr:
  • echo 'Starting the setup of your laptop'\''s wireless card.' Starting the setup of your laptop's wireless card.
  • '[' -d ./MyWirelessCard ']'
  • mkdir ./MyWirelessCard
  • FILE=./MyWirelessCard/fedora23_broadcom_wl_install.sh ++ stat -c %u ./MyWirelessCard/fedora23_broadcom_wl_install.sh stat: cannot stat ‘./MyWirelessCard/fedora23_broadcom_wl_install.sh’: No such file or directory
  • uname1=' '
  • '[' ' = 1000' ']'
  • '[' ' -r -w -x ./MyWirelessCard/fedora23_broadcom_wl_install.sh ' ']'
  • echo 'Preforming initial system update.' Preforming initial system update.
  • dnf -y update Error: This command has to be run under the root user.
  • echo Script is finished Script is finished
  • exit

Eu estou perdido Nenhuma ajuda seria muito apreciada.

    
por Purpe_Fedora 02.05.2016 / 22:47

1 resposta

3

Você teria que alterar os diretórios no seu script. Tipicamente:

cd "$(dirname "$0")"
    
por 02.05.2016 / 22:58