PDA

View Full Version : Running a Bash script in terminal...



eeeguy
January 10th, 2010, 08:30 PM
Say I have written/copy&pasted a bash script into gedit, what do I save it as to actually make it a bash script file? And once I have done that, how do I run the script?

LinuxRules1
January 10th, 2010, 08:36 PM
if you write a bash script in gedit you save it as a .sh file. then you just go to terminal and do the following,

give 777 permissions


chmod 777 file.sh

make it executable


chmod +x file.sh

hope this helps:)

eeeguy
January 10th, 2010, 08:41 PM
if you write a bash script in gedit you save it as a .sh file. then you just go to terminal and do the following,

give 777 permissions


chmod 777 file.shmake it executable


chmod +x file.shhope this helps:)

Okay thanks, Ive done that. :D Now, how do I run the script?

Tahakki
January 10th, 2010, 08:42 PM
Run ./file.sh

eeeguy
January 10th, 2010, 08:49 PM
Run ./file.sh
Uh, I typed "Run ./mystupidfile.sh" and "run ./mystupidfile.sh" Both came up as "Run: command not found"

Hellkeepa
January 10th, 2010, 08:50 PM
HELLo!

I recommend using "chmod u+x file.sh" instead, that way only you can run it.
The first line given by LinuxRules1 gives everyone full permissions on teh file; Read, write, execute. Making this very insecure.
The second line adds execute rights to everyone, so that they can't change it, but still run it.

Edit, added: He meant it without the "run" bit, as in "run the command...."

Happy codin'!

eewoz
January 11th, 2010, 01:43 AM
Uh, I typed "Run ./mystupidfile.sh" and "run ./mystupidfile.sh" Both came up as "Run: command not found"

If you are in the directory that contains mystupidfile.sh then just type ./mystupidfile.sh at the command prompt. You should not type Run.

eeeguy
January 11th, 2010, 03:03 PM
If you are in the directory that contains mystupidfile.sh then just type ./mystupidfile.sh at the command prompt. You should not type Run.
Oh right yeah, cant expect it to find the file for me can I?
But uh..how do you do that? And when going to my home directory from where ever it is by defalut do I type "home" for my home folder or my username?

Hellkeepa
January 11th, 2010, 03:15 PM
HELLo!

"cd <folder>" to change directories, you can use either static or relative paths. Just "cd" alone brings you to your home folder, but you can also use "cd ~" (tilde).

Happy codin'!

SecretCode
January 11th, 2010, 03:30 PM
If you're in a different directory and the file is in your home directory you can type

~/mystupidfile.sh
or

/home/eeeguy/mystupidfile.sh

I'm sure you can work out what to type if you're in your home directory and the script is in a subdirectory like scripts...

scripts/mystupidfile.sh

By the way, calling it file.sh helps but isn't necessary. It doesn't have to have an extension at all.

fluffen
January 11th, 2010, 09:20 PM
Whenever i want to run a bash script i simply type
bash scriptname.sh