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.

  • 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 May 2 '16 at 21:06
  • Related reading – Daniel B May 2 '16 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" – glenn jackman May 2 '16 at 21:23
  • If you have to create that directory, then the $FILE will certainly not exist. – glenn jackman May 2 '16 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 – Purpe_Fedora May 2 '16 at 21:26
3

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

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.