The Terminal/Console:BASH SCRIPT

Learn how to install via the terminal.
User avatar

Topic Author
Blackwolf
Forum Administrator
Forum Administrator
Posts: 8522
Joined: Sun Dec 13, 2009 6:07 pm
Has thanked: 6 times
Status: Offline

BASH SCRIPT

Postby Blackwolf » Thu Apr 01, 2010 6:24 am

Keeping it simple: You know by now that "rm" removes a file, permanently !
Wouldn't it be nice if we could move it to the recycle bin with a simple command instead ?

We're gonna make that command and call it: "del"
( YES ! making our own commands ! )

First a little script:


Code: Select all

$ vi /usr/bin/del


"i"
Here is the text for the script:

Code: Select all

#!/bin/bash
mv $1 ~/Desktop/Trash
#End script



< Esc >
"ZZ"

Make it executable


Code: Select all

# chmod 0775 /usr/bin/del



Now if we do


Code: Select all

$ del tessst


( It will execute the script and do the same as: )


Code: Select all

$ mv tessst /home/bruno/Desktop/Trash



Sure this was a very short example, a 3 line script, it only holds one command, but you could put as many lines in the script as you want and execute it with a four letter word.
If there are more commands in the script it will execute them in the order that they are noted down.

Because /usr/bin is in your "path" you only have to type "del" to execute it.

If you have to do complicated commands in a certain order on a regular basis, make a little bash script, put it in your "path" and give it a name that's easy to remember.

Next time we'll make a simple backup script, to backup and gzip the contents of your /home directory.
System: Host: Arch Kernel: 4.0.4-1-MANJARO x86_64 (64 bit gcc: 4.9.2)
Desktop: Xfce 4.12.2 (Gtk 2.24.28)
nVidia EVGA Geforce GTX580 1536MB
Intel Core i7 2600K Quad Core Overclocked to 4.4GHz
Gigabyte Z68 UD7 Chipset, Corsair 16GB Kit (4x4GB) 1600MHz DDR3, OCZ 120GB Vertex 3 SSD
Corsair 8GB Kit (2x4GB) 1600MHz DDR3, OCZ 120GB Agility 3 SSD (WD 1TB Caviar SATA-III HDD)

HP DV6 Laptop 8x Intel(R) Core(TM) i7-2630QM CPU @ 2.00GHz
Oz Unity Diamond IIB

Add us on our as well as our
then follow us on !!
Image
Image

Image


Image

User avatar

UltimateCat3
Forum Member
Forum Member
Posts: 1060
Joined: Tue Dec 13, 2011 7:42 am
Status: Offline

Re: BASH SCRIPT

Postby UltimateCat3 » Sun May 26, 2013 1:56 pm

Can I use :

Code: Select all

chmod 0775 path to and name of file

for all scripts to be executable?

What is chmod +x for? A dot run file?
Laptop: Sony Vaio
Intel i5 6GB RAM 1GB Radeon Turbo Boost
64-Bit architecture
CentOS

Gaming PC
MSI z87GD65 i7 Intel
MSI R7850 Twin Frozer 2GB GDDR5 Radeon
Triple Boot
Black Opal 64, Elementary OS & Linux Mint
Image

http://sites.google.com/site/ruledbytheheartart

"If it ain't broke don't fix it; it you can't fix it don't break it; you reap what you tweak"

User avatar

Topic Author
Blackwolf
Forum Administrator
Forum Administrator
Posts: 8522
Joined: Sun Dec 13, 2009 6:07 pm
Has thanked: 6 times
Status: Offline

Re: BASH SCRIPT

Postby Blackwolf » Sun May 26, 2013 2:02 pm

What is the difference between chmod u+x and just chmod +x? I have seen a ton of tutorials that say to use u+x to make scripts executable. However, the u is not mentioned in the chmod help or manual. Omitting the u doesn't seem to have any effect either. Is it just a deprecated argument? Thanks.


The man page of chmod covers that.
u stands for user.
g stands for group.
o stands for others.
a stands for all.

That means that chmod u+x somefile will grant only the owner of that file execution permissions whereas chmod +x somefile is the same as chmod a+x somefile

chmod u+x will made the file executable for your user (it will only add it for your user, though it may be already executable by the group owner, or "other").

chmod +x or chmod a+x ('all plus executable bit') makes the file executable by everyone.

If you do this to a directory, it makes the directory searchable, instead. I.e., you can list the contents of a directory that you have +x permission on.
System: Host: Arch Kernel: 4.0.4-1-MANJARO x86_64 (64 bit gcc: 4.9.2)
Desktop: Xfce 4.12.2 (Gtk 2.24.28)
nVidia EVGA Geforce GTX580 1536MB
Intel Core i7 2600K Quad Core Overclocked to 4.4GHz
Gigabyte Z68 UD7 Chipset, Corsair 16GB Kit (4x4GB) 1600MHz DDR3, OCZ 120GB Vertex 3 SSD
Corsair 8GB Kit (2x4GB) 1600MHz DDR3, OCZ 120GB Agility 3 SSD (WD 1TB Caviar SATA-III HDD)

HP DV6 Laptop 8x Intel(R) Core(TM) i7-2630QM CPU @ 2.00GHz
Oz Unity Diamond IIB

Add us on our

as well as our




then follow us on

!!
Image
Image

Image


Image

User avatar

pam
Forum Moderator
Forum Moderator
Posts: 507
Joined: Wed May 23, 2012 2:42 pm
Been thanked: 1 time
Status: Offline

Re: BASH SCRIPT

Postby pam » Sun May 26, 2013 2:16 pm

PILEDRIVER Quad core APU laptop : Hp pavillion g6 2005AX, OS: Debian Rolling.
Image

User avatar

UltimateCat3
Forum Member
Forum Member
Posts: 1060
Joined: Tue Dec 13, 2011 7:42 am
Status: Offline

Re: BASH SCRIPT

Postby UltimateCat3 » Sun May 26, 2013 2:33 pm

I understand now about chmod +x and a+x thanks-

I read a few articles online about stderr and stdout and all that did is cause more confusion.
Having trouble understanding how to write scripts and what scripting really is all about.
I managed to make the simple "Hello World" script but if I had to write a script for a cronjob I would not know how to-

Are the man pages the right place to learn how to write a bash script?
Laptop: Sony Vaio
Intel i5 6GB RAM 1GB Radeon Turbo Boost
64-Bit architecture
CentOS

Gaming PC
MSI z87GD65 i7 Intel
MSI R7850 Twin Frozer 2GB GDDR5 Radeon
Triple Boot
Black Opal 64, Elementary OS & Linux Mint
Image

http://sites.google.com/site/ruledbytheheartart

"If it ain't broke don't fix it; it you can't fix it don't break it; you reap what you tweak"

User avatar

pam
Forum Moderator
Forum Moderator
Posts: 507
Joined: Wed May 23, 2012 2:42 pm
Been thanked: 1 time
Status: Offline

Re: BASH SCRIPT

Postby pam » Sun May 26, 2013 2:36 pm

Man page references are a no-nonsense way to write scripts but provided you know the basics, otherwise you're looking at greek.
PILEDRIVER Quad core APU laptop : Hp pavillion g6 2005AX, OS: Debian Rolling.
Image

User avatar

UltimateCat3
Forum Member
Forum Member
Posts: 1060
Joined: Tue Dec 13, 2011 7:42 am
Status: Offline

Re: BASH SCRIPT

Postby UltimateCat3 » Sun May 26, 2013 2:40 pm

pam wrote:http://www.tuxfiles.org/linuxhelp/filepermissions.html


That's a good tuxfiles webpage; Thanks Pam!

This is my second go round with trying to understand how Bash Scripting and all of the practices, rules, arguments and etc. go and work.
It's not exactly the easiest thing to pick up on-

I hope that persistance wins cause I still don't get it-- :grrr
Laptop: Sony Vaio
Intel i5 6GB RAM 1GB Radeon Turbo Boost
64-Bit architecture
CentOS

Gaming PC
MSI z87GD65 i7 Intel
MSI R7850 Twin Frozer 2GB GDDR5 Radeon
Triple Boot
Black Opal 64, Elementary OS & Linux Mint
Image

http://sites.google.com/site/ruledbytheheartart

"If it ain't broke don't fix it; it you can't fix it don't break it; you reap what you tweak"

User avatar

pam
Forum Moderator
Forum Moderator
Posts: 507
Joined: Wed May 23, 2012 2:42 pm
Been thanked: 1 time
Status: Offline

Re: BASH SCRIPT

Postby pam » Sun May 26, 2013 2:52 pm

The old saying---
"The only way to write code, is to Look at it."

Dont bother much with understanding or its meaning, know what is does first, the theory(understanding and meaning) part is basically just 'intuition'.

If you want to see the core of the sun...here is a little something:
All commands | commandlinefu.com
PILEDRIVER Quad core APU laptop : Hp pavillion g6 2005AX, OS: Debian Rolling.
Image

User avatar

UltimateCat3
Forum Member
Forum Member
Posts: 1060
Joined: Tue Dec 13, 2011 7:42 am
Status: Offline

Re: BASH SCRIPT

Postby UltimateCat3 » Sun May 26, 2013 3:48 pm

pam wrote:The old saying---
"The only way to write code, is to Look at it."

Dont bother much with understanding or its meaning, know what is does first, the theory(understanding and meaning) part is basically just 'intuition'.

If you want to see the core of the sun...here is a little something:
All commands | commandlinefu.com


Looking at it was Greek to me-- but none the less I am going thru that entire page that you linked for me.
The black background with green lettering helps. Thank You.

I'll keep looking like you said.
Eventually after looking at code and reading it enough will it make sense and I will than understand ? ?
Laptop: Sony Vaio
Intel i5 6GB RAM 1GB Radeon Turbo Boost
64-Bit architecture
CentOS

Gaming PC
MSI z87GD65 i7 Intel
MSI R7850 Twin Frozer 2GB GDDR5 Radeon
Triple Boot
Black Opal 64, Elementary OS & Linux Mint
Image

http://sites.google.com/site/ruledbytheheartart

"If it ain't broke don't fix it; it you can't fix it don't break it; you reap what you tweak"

User avatar

pam
Forum Moderator
Forum Moderator
Posts: 507
Joined: Wed May 23, 2012 2:42 pm
Been thanked: 1 time
Status: Offline

Re: BASH SCRIPT

Postby pam » Sun May 26, 2013 5:25 pm

Download bash cookbook free pdf:
bash Cookbook - Free Download eBook - pdf

from the basics to advanced, with examples and no unecessary theory.
PILEDRIVER Quad core APU laptop : Hp pavillion g6 2005AX, OS: Debian Rolling.
Image


Return to “The Terminal/Console:”

Who is online

Users browsing this forum: CommonCrawl [Bot] and 0 guests