Changing my shell bash script default $PWD to it's (the script's) current location

1

Hey I'm running my shell script that interacts with folders and files, but my $PWD that it keeps pointing to /home/MyUserName directory and this messes with what it is supposed to do. I am running 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

I get:

  • 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

I am at a loss any help would be greatly appreciated.

Purpe_Fedora

Posted 2016-05-02T20:47:56.917

Reputation: 67

Unless I'm missing something, the sudo su stuff is pointless. It's probably not good practice anyway, it's enough to complain about not running as root and exit. /edit: Ah, you're relying on authentication caching. That's even worse. :D – Daniel B – 2016-05-02T21:06:01.200

Related reading – Daniel B – 2016-05-02T21:11:29.720

Your quoting is wrong in many places. For example [ " -r -w -x $FILE " ] needs to be [ -r "$FILE" ] && [ -w "$FILE" ] && [ -x "$FILE" ] -- your code will never be "false" – glenn jackman – 2016-05-02T21:23:03.107

If you have to create that directory, then the $FILE will certainly not exist. – glenn jackman – 2016-05-02T21:26:21.780

Interesting, but just out of curiosity, what is the difference? It does it's job for me, at least as far as I know – Purpe_Fedora – 2016-05-02T21:26:38.830

Answers

3

You'd have to actually change directories in your script. Typically:

cd "$(dirname "$0")"

glenn jackman

Posted 2016-05-02T20:47:56.917

Reputation: 18 546

I read somewhere that you can't use that if it contains the file, or is that about another command? – Purpe_Fedora – 2016-05-02T21:09:31.303

2I don't understand your question. – glenn jackman – 2016-05-02T21:10:29.767

When I use $PWD, it shows my user folder, not the script folder. – Purpe_Fedora – 2016-05-02T21:13:01.667

The script folder is dirname "$0". After you cd there, $PWD will have the desired value. – glenn jackman – 2016-05-02T21:17:44.497

Thanks it took a bit for me to put the peices together, and the related reading helped add to it that saves the last few remaining of my hairs left! :) – Purpe_Fedora – 2016-05-02T21:20:43.520