Page 1 of 2

??? What am I missing?? Newbie scripting err. (Solved)

Posted: 18 Aug 2014, 18:51
by Ed_P
I'm going crazy here. I have a simple task, copy a file from one location to another. I have several scripts that do similar things with no problem but this one keeps erring out. Can anyone see what I'm doing wrong?

Code: Select all

#!/bin/sh 

if [ `whoami` != "root" ]; then
  ktsuss "$0"
  exit
fi 
 
echo 
cp -u -v /home/guest/smb.conf /etc/samba/
Impressive eh?

And this is what I see in terminal mode.

Code: Select all

guest@porteus:~$ sh smb.sh
sudo: smb.sh: command not found       <-- this after I respond to the root password prompt

guest@porteus:~$ cp smb.conf /etc/samba/
cp: cannot create regular file ‘/etc/samba/smb.conf’: Permission denied
guest@porteus:~$ su
Password: 
root@porteus:/home/guest# cp -u -v smb.conf /etc/samba/
root@porteus:/home/guest# 

Re: ??? What am I missing??

Posted: 18 Aug 2014, 19:31
by brokenman
You are in the habit of running a script with 'sh myscript.sh' and you haven't made your smb.sh script executable.

Re: ??? What am I missing??

Posted: 18 Aug 2014, 20:03
by Ed_P
Aw crap. You're right. Somehow I've picked up the thought that if I use the sh command I don't need to do the chmod +x command. And it is partially true, the test of root and the prompt part of the script work.

Thank you brokenman.

Re: ??? What am I missing?? Newbie scripting err. (Solved)

Posted: 19 Aug 2014, 00:00
by brokenman
You're welcome. Not only do you not need the executable bit set, if you run the script with 'sh' you don't even need the shebang (#!/bin/sh) at the top since you're telling sh to run it. I would even say it is safer not to set the executable bit on a script but it has its place.

Re: ??? What am I missing?? Newbie scripting err. (Solved)

Posted: 19 Aug 2014, 04:15
by Ed_P
brokenman wrote:Not only do you not need the executable bit set, if you run the script with 'sh' you don't even need the shebang (#!/bin/sh) at the top since you're telling sh to run it.
:%) But you said:
brokenman wrote:You are in the habit of running a script with 'sh myscript.sh' and you haven't made your smb.sh script executable.
:crazy:

I'm thoroughly confused at this point. If I don't make a script executable, it doesn't work and you tell me I need to make it executable. Then you say I don't need to if I run it with the 'sh' command which is what I was doing when it failed to run. :wall:

BTW I remember why I thought I didn't need to make a script executable when using the 'sh' command. When I run scripts in Always Fresh mode I am running them from copies of my /home/guest/ versions stored on a NTFS partition which I suspect is not supporting the script's executable bit.

So, if I use the 'sh' command I don't need to make a script executable then why did my basic task script fail? :unknown:

(BTW The main purpose of the simple script is to remind me where the file needs to go. And that it needs root access.)

Re: ??? What am I missing?? Newbie scripting err. (Solved)

Posted: 20 Aug 2014, 01:21
by brokenman
and you tell me I need to make it executable.
I didn't say that you NEED to make it executable. This is an option and in some cases the best option. This is what I meant by 'it has its place'. In this case when a script recalls itself it is much easier to make the script executable. If you don't then you need to make the script recall itself using sh. For example (I am in kde right now) kdesu -c 'sh $0' Wouldn't it be easier to just use 'su' instead of opening a gui to enter your password?
In that case it would be something like: su - -c 'sh $0'
Then you say I don't need to if I run it with the 'sh' command which is what I was doing when it failed to run.
Take a closer look at your script. When YOU invoke the script you are using 'sh' but when the script sees you are not root, it reruns ITSELF (using ktsuss) without using 'sh'

Regarding running scripts from a windows partition. You don't the shebang or the executable bit set. Try it to see for yourself:

Code: Select all

echo "echo working" > /mnt/sda4/test
/mnt/sda4/test
/mnt/sda4 in this case is any FAT ro NTFS partition

Re: ??? What am I missing?? Newbie scripting err. (Solved)

Posted: 20 Aug 2014, 03:09
by Ed_P
brokenman wrote:Regarding running scripts from a windows partition. You don't the shebang or the executable bit set. Try it to see for yourself:

Code: Select all

echo "echo working" > /mnt/sda4/test
/mnt/sda4/test
/mnt/sda4 in this case is any FAT ro NTFS partition

Code: Select all

guest@porteus:~$ echo "echo working" > /mnt/sda5/test
guest@porteus:~$ /mnt/sda5/test
working
guest@porteus:~$ 
:shock: Wow!!
Take a closer look at your script. When YOU invoke the script you are using 'sh' but when the script sees you are not root, it reruns ITSELF (using ktsuss) without using 'sh'
Ok, but the test above shows I don't need the 'sh' or executable bit to run, or rerun, a script so why did the script fail?

I think this is getting to be a little bit beyond me. :sorry:

Re: ??? What am I missing?? Newbie scripting err. (Solved)

Posted: 20 Aug 2014, 03:39
by brokenman
Because in the example you showed originally you are not on a windows partition. You are in your $HOME folder on aufs. Set the executable bit and all will be good.

Re: ??? What am I missing?? Newbie scripting err. (Solved)

Posted: 20 Aug 2014, 15:27
by Ed_P
Ah so Image master.

:)

Re: ??? What am I missing?? Newbie scripting err. (Solved)

Posted: 20 Aug 2014, 18:23
by Bogomips
Indeed fortunate for such mentors. :Yahoo!:

Re: ??? What am I missing?? Newbie scripting err. (Solved)

Posted: 21 Aug 2014, 00:31
by Ed_P
brokenman wrote:For example (I am in kde right now) kdesu -c 'sh $0' Wouldn't it be easier to just use 'su' instead of opening a gui to enter your password?
In that case it would be something like: su - -c 'sh $0'
When I execute a script in /home/guest/ and use 'ktsuss "$0"' I stay in "guest@porteus:~$" but when I use "su - -c 'sh $0'" I end up in "root@porteus:~#".

Code: Select all

guest@porteus:~$ test.sh
Password: 
root@porteus:~# 
root@porteus:~# 
root@porteus:~# exit
exit
guest@porteus:~$ 
test.sh

Code: Select all

#!/bin/sh 

if [ `whoami` != "root" ]; then
  su - -c 'sh $0'
  exit
fi
echo Where am I?
read

if [ `whoami` != "root" ]; then
  ktsuss "$0"
  exit
fi
echo Now where am I?
read
exit
Bogomips wrote:Indeed fortunate for such mentors. :Yahoo!:
Indeed. :)

Re: ??? What am I missing?? Newbie scripting err. (Solved)

Posted: 21 Aug 2014, 01:32
by brokenman
In that case remove the dash.
su -c 'sh $0'

Re: ??? What am I missing?? Newbie scripting err. (Solved)

Posted: 21 Aug 2014, 01:43
by Ed_P
Now I get

Code: Select all

guest@porteus:~$ test.sh
Password: 
/bin/bash: /bin/bash: cannot execute binary file
guest@porteus:~$ 
File Properties shows the Type as "shell script" and "applications/x-shellscript" so I'm reasonably sure it's executable. I put quotes around the echos so it's nothing in there.

Re: ??? What am I missing?? Newbie scripting err. (Solved)

Posted: 21 Aug 2014, 02:44
by brokenman
This worked fine for me.

Code: Select all

guest@porteus:~$ cat test.sh 
#!/bin/sh 

if [ `whoami` != "root" ]; then
  su -c "sh $0"
  exit
fi
echo "Where am I?"
read

guest@porteus:~$ ls -l test.sh 
-rw-r--r-- 1 guest guest 93 Aug 20 23:39 test.sh  <---- not executable because no 'x' in the rwx

guest@porteus:~$ sh test.sh 
Password: 
Where am I?

guest@porteus:~$

Re: ??? What am I missing?? Newbie scripting err. (Solved)

Posted: 21 Aug 2014, 03:00
by Ed_P

Code: Select all

guest@porteus:~$ ls -l test.sh
-rwxr-xr-x 1 guest guest 600 Aug 20 21:35 test.sh*
guest@porteus:~$ sh test.sh
Password: 
/bin/bash: /bin/bash: cannot execute binary file
guest@porteus:~$

Code: Select all

#!/bin/sh 

if [ `whoami` != "root" ]; then
  su -c 'sh $0'
  exit
fi
echo "Where am I?"
read

if [ `whoami` != "root" ]; then
  ktsuss "$0"
  exit
fi
echo "Now where am I?"
read
exit
And with a non-x version

Code: Select all

guest@porteus:~$ ls -l testX.sh
-rw-r--r-- 1 guest guest 183 Aug 20 22:51 testX.sh
guest@porteus:~$ sh testX.sh
Password: 
/bin/bash: /bin/bash: cannot execute binary file
guest@porteus:~$ cat testX.sh
#!/bin/sh 

if [ `whoami` != "root" ]; then
  su -c 'sh $0'
  exit
fi
echo "Where am I?"
read

if [ `whoami` != "root" ]; then
  ktsuss "$0"
  exit
fi
echo "Now where am I?"
read
exitguest@porteus:~$ 
the same thing.

What the heck?? :unknown: