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.

5
  • 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
    Commented May 2, 2016 at 21:06
  • Related reading
    – Daniel B
    Commented May 2, 2016 at 21:11
  • 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" Commented May 2, 2016 at 21:23
  • If you have to create that directory, then the $FILE will certainly not exist. Commented May 2, 2016 at 21:26
  • Interesting, but just out of curiosity, what is the difference? It does it's job for me, at least as far as I know Commented May 2, 2016 at 21:26

1 Answer 1

3

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

cd "$(dirname "$0")"
5
  • I read somewhere that you can't use that if it contains the file, or is that about another command? Commented May 2, 2016 at 21:09
  • 2
    I don't understand your question. Commented May 2, 2016 at 21:10
  • When I use $PWD, it shows my user folder, not the script folder. Commented May 2, 2016 at 21:13
  • The script folder is dirname "$0". After you cd there, $PWD will have the desired value. Commented May 2, 2016 at 21:17
  • 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! :) Commented May 2, 2016 at 21:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .