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

Post here if you are a new Porteus member and you're looking for some help.
User avatar
Ed_P
Contributor
Contributor
Posts: 3150
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

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

Post#1 by Ed_P » 18 Aug 2014, 18:51

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# 
Last edited by Ed_P on 18 Aug 2014, 20:04, edited 1 time in total.
Ed

User avatar
brokenman
Site Admin
Site Admin
Posts: 5456
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v3.2rcX all desktops
Location: Brazil
Contact:

Re: ??? What am I missing??

Post#2 by brokenman » 18 Aug 2014, 19:31

You are in the habit of running a script with 'sh myscript.sh' and you haven't made your smb.sh script executable.
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
Ed_P
Contributor
Contributor
Posts: 3150
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

Re: ??? What am I missing??

Post#3 by Ed_P » 18 Aug 2014, 20:03

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.
Ed

User avatar
brokenman
Site Admin
Site Admin
Posts: 5456
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v3.2rcX all desktops
Location: Brazil
Contact:

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

Post#4 by brokenman » 19 Aug 2014, 00:00

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.
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
Ed_P
Contributor
Contributor
Posts: 3150
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

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

Post#5 by Ed_P » 19 Aug 2014, 04:15

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.)
Ed

User avatar
brokenman
Site Admin
Site Admin
Posts: 5456
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v3.2rcX all desktops
Location: Brazil
Contact:

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

Post#6 by brokenman » 20 Aug 2014, 01:21

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
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
Ed_P
Contributor
Contributor
Posts: 3150
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

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

Post#7 by Ed_P » 20 Aug 2014, 03:09

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:
Ed

User avatar
brokenman
Site Admin
Site Admin
Posts: 5456
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v3.2rcX all desktops
Location: Brazil
Contact:

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

Post#8 by brokenman » 20 Aug 2014, 03:39

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.
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
Ed_P
Contributor
Contributor
Posts: 3150
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

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

Post#9 by Ed_P » 20 Aug 2014, 15:27

Ah so Image master.

:)
Ed

Bogomips
Full of knowledge
Full of knowledge
Posts: 2536
Joined: 25 Jun 2014, 15:21
Distribution: 3.2.2 Cinnamon & KDE5
Location: London

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

Post#10 by Bogomips » 20 Aug 2014, 18:23

Indeed fortunate for such mentors. :Yahoo!:
Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

User avatar
Ed_P
Contributor
Contributor
Posts: 3150
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

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

Post#11 by Ed_P » 21 Aug 2014, 00:31

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. :)
Ed

User avatar
brokenman
Site Admin
Site Admin
Posts: 5456
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v3.2rcX all desktops
Location: Brazil
Contact:

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

Post#12 by brokenman » 21 Aug 2014, 01:32

In that case remove the dash.
su -c 'sh $0'
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
Ed_P
Contributor
Contributor
Posts: 3150
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

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

Post#13 by Ed_P » 21 Aug 2014, 01:43

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.
Ed

User avatar
brokenman
Site Admin
Site Admin
Posts: 5456
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v3.2rcX all desktops
Location: Brazil
Contact:

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

Post#14 by brokenman » 21 Aug 2014, 02:44

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:~$
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
Ed_P
Contributor
Contributor
Posts: 3150
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 3.2.2 64-bit ISO
Location: Western NY, USA

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

Post#15 by Ed_P » 21 Aug 2014, 03:00

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:
Ed

Post Reply