LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Fedora (https://www.linuxquestions.org/questions/fedora-35/)
-   -   Bash shell scripting question. (https://www.linuxquestions.org/questions/fedora-35/bash-shell-scripting-question-430376/)

$Linuxnoob 03-31-2006 12:58 PM

Bash shell scripting question.
 
when I'm attempting to write my own shell script after I've written it where should I save it to to be executable?

Also is the scarlar sign the one you use to execute?


$script.sh

and .sh for bash is the correct ending yes?

acid_kewpie 03-31-2006 01:04 PM

all of your questions are actually illogical or irrelevant, if i'm understanding the 2nd one right in the first place...

1) you can put a script *anywhere* there is no special location.

2) you mean the $ sign? no, that is just an example command prompt. it is used in code to clarify exactly how to run a program.

3) this is not windows, files are recognized by what they actaully are, not what characters just so happen to be on the end of it. the use of .sh is a handy visual convention, but absolutely non essential

i guess you're just asking how to make a script exectuable? run "chmod +x scriptname" and then you canjust run the script as if it were a binary executable.

macemoneta 03-31-2006 01:05 PM

The location and name of the file are not important. The location is usually in a bin directory. On your userid, in ~/bin/ which should be in your default PATH:

echo $PATH

The file extension is not used on Linux, it's is strictly for your information. The executable could be called 'linuxnoob.sh', 'linuxnoob.xyz' or just 'linuxnoob' - it doesn't make any difference.

To make a shell script executable, two things are needed:

1. The script must start with #! followed by the full path to the shell interpreter. For example:

#!/bin/bash

2. The executable flag must be set on the file. For example:

chmod 755 somescript

$Linuxnoob 03-31-2006 03:43 PM

How do you CHMOD a file to 755 through CLI?

macemoneta 03-31-2006 03:51 PM

Quote:

Originally Posted by $Linuxnoob
How do you CHMOD a file to 755 through CLI?

You open a terminal, and at the prompt issue the command:

chmod 755 filename

Replace 'filename' with the name of the file you want to chmod. The '755' means:

rwxr-xr-x

Which expands to:

Owner permissions: rwx (binary 111=7)
Group permissions: r-x (binary 101=5)
Others permissions: r-x (binary 101=5)

If you don't want others in your group or other users on the system to be able to access the file, then use 700 instead.

$Linuxnoob 03-31-2006 03:52 PM

Thanks a lot. I tried to CHMOD before, but never had luck hehe, you've been a lot of help.

$Linuxnoob 03-31-2006 03:53 PM

[root@ip-181-157 ~]# chmod 755 script
chmod: cannot access `script': No such file or directory


I tried as you said, but it returned this, am I doing something wrong?

acid_kewpie 03-31-2006 03:55 PM

well you've got to be in the right directory to reach the file... or use an absolute or relative file path e.g. /home/user/script or ../user/script etc...

macemoneta 03-31-2006 03:56 PM

If the file 'script' is not in the current directory, then you must either cd to the correct directory, or specify the path. For example, if 'script is in the '~/bin/' directory, you can either:

cd ~/bin
chmod 755 script

or

chmod 755 ~/bin/script

$Linuxnoob 03-31-2006 03:59 PM

Ah, excellent, now to stop the program from CLI you'd use something to the effect of kill ? or what would you use?

PTrenholme 03-31-2006 04:09 PM

Are you running as "root"?:tisk:

This should never be necessary unless you're making significant system changes. Or inviting strangers to use your system for anything they want to do.

Read man sudo and man sudoers to see how you can permit your "standard" login to execute privileged commands.

As to you problem, script must be the full path to the actual script name and location.

$Linuxnoob 03-31-2006 04:45 PM

Oh thanks for the help. I will read those and start using my other name.

If for instance i had someof my poetry on root in fileA and someof my school assignments in fileB is there anyway I can make those files viewable for other users, which consequently is me and myself hehe.

PTrenholme 04-01-2006 07:55 AM

Sure. use the mv command and the chown command. For example:
Code:

$ sudo mv /root/<Poetry File> /home/<me>/<Poetry Directory>/
$ sudo chown <me>:<my group> /home/<me>/<Poetry Directory>/<Poetry File>

Where, of course, you need to replace everything in angle brackets (including the brackets) by the actual names you use.

See man mv and man chown for details.

Also, note that info --apropos <keywords> and man -K <keywords> will search the on-line documentation for any command whose description uses that keyword. (And, of course, info info and man man will give you details about the info and man commands.

$Linuxnoob 04-01-2006 01:12 PM

Thanks for the assistance.


All times are GMT -5. The time now is 05:43 PM.