LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-06-2004, 08:15 PM   #1
tradewind
LQ Newbie
 
Registered: Jun 2004
Location: Hanoi, Vietnam
Distribution: RedHat
Posts: 20

Rep: Reputation: 0
Is there any difference between 2 shell scripts?


Hi all,
I have 2 shell scripts
1. (shutdown1.sh)
#!/bin/sh
/etc/security/console.apps/poweroff

2. (shutdown2.sh)
#!/bin/sh
cd /etc/security/console.apps
poweroff

It's OK when using shutdown2 to shutdown the computer but when using shutdown1, I get the warning: permission denied
Can anyone help me?
 
Old 06-06-2004, 08:38 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
There is a subtle difference between the two of them.

In the first script, you use the absolute path to the command. So, /etc/security/console.apps/poweroff is executed with 100% certainty.

In the second script you cd to /etc/security/console.apps, and then execute the poweroff command. This probably doesn't do what you think it is. By default, most linux distributions do not include the current directory as part of your path. So changing directories will not influence what program is executed. The system would look through the directories listed in your PATH environment variable, and execute the first copy of "poweroff" it finds in those directories. To guarantee the poweroff command in the console.apps is executed, you should change "poweroff" to "./poweroff".

If you have modified your PATH environment variable to include the current directory, then the only explanation I can think of would be that the poweroff command is looking for a config or data file using a relative path (like ../somefile for instance).

To know what poweroff command you use in the second script, type which poweroff at a command prompt. If it says anything other than /etc/security/console.apps/poweroff, then your second script isn't executing the same poweroff command as your first one is.
 
Old 06-07-2004, 01:33 AM   #3
tradewind
LQ Newbie
 
Registered: Jun 2004
Location: Hanoi, Vietnam
Distribution: RedHat
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks Helmet for your reply!
The problem is when I log in as test, I can only use shutdown2. Each time I execute shutdown1, I get the following error: "permission denied".
 
Old 06-07-2004, 02:42 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
In this setup, a permission problem can come from one of two places:
1. permissions for executing the script itself
2. permissions for executing the program in the script

My previous post was meant to address #2. I felt that you might have overlooked the possibility that the two scripts were potentially executing two different commands (both named poweroff but located in different places). You could get a permission denied error unless both poweroff commands had the exact same permissions.

To address #1 is similar. Each script has its own set of permissions. You use the chmod command to change a file's permissions. Below is a rundown of how permissions are organized. If you're familiar with that info, then all you probably need to do is execute man chmod and read up on how to change the permissions.

Every file in linux (data file, program, script, etc.) has three sets of permissions: user, group, and other. You can see what permissions are allowed with the "ls -l" command. For instance, you might see something like this (minus the colors):
Code:
$ ls -l  test
-rwxr-xr--    7 tradew   users        4096 Jun  5 17:00 test
The colors indicate which which users have which permissions.

Green is for the file's owner (in this case: tradew)
Red is for the file's group (in this case: users)
Blue is for anybody else (not the owner and not in the assigned group)

'r' means read; 'w' means write; 'x' means execute; '-' means no permission for that option.

From above, the owner can read, write, and execute the file. Anyone in the group: users can read or execute (but not write) the file, and anyone else can only read the file.

When you do an "ls -l" on your scripts, figure out where your test user fits. Then change the permissions appropriately. For safety, I would give read and execute to whichever set of permissions apply to test. If test is the owner:
chmod u+rx script_name

If test in in the group:
chmod g+rx script_name

If test is in the "others":
chmod o+rx script_name

Note: you have to be root or the file's owner to change the permissions.

Last edited by Dark_Helmet; 06-07-2004 at 02:45 AM.
 
Old 06-07-2004, 09:16 AM   #5
villie
Member
 
Registered: Apr 2004
Location: Pakistan
Distribution: RedHat
Posts: 46

Rep: Reputation: 15
Maybe you have the permission to execute the script but you dont have permission to change to that directory where the script is located .I n that case you will have to change permission for that directory as well. Most probably you will have to use ./poweroff ... because current directory will not be in the path and should generally not be in the PATH for security reasons.
Villie
 
Old 06-07-2004, 07:52 PM   #6
tradewind
LQ Newbie
 
Registered: Jun 2004
Location: Hanoi, Vietnam
Distribution: RedHat
Posts: 20

Original Poster
Rep: Reputation: 0
The problem is that 'poweroff' command is located in different places.
Thanks for your helps!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between normal shell and login shell Manish Linux - General 7 11-14-2012 08:30 AM
Need help with shell scripts thorney Linux - Newbie 4 11-27-2005 11:18 PM
Shell scripts??? F_ANTHONY Programming 2 10-27-2004 06:28 PM
difference of shell redragon7964 Linux - Newbie 2 04-06-2004 02:20 AM
shell scripts rocketgo Linux - General 10 12-01-2003 05:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration