Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Bash Scripting

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
DFC302
Posts: 4
Joined: 2015-08-07 19:13

Bash Scripting

#1 Post by DFC302 »

Hi, sorry if this is the wrong place to post this. I am still very new to linux and I was interested in learning how to write bash scripts. I tried searching for this topic but couldn't find anything in relation. I searched google but alot of the tutorials are dry and not very helpful. Does anyone have any recommendations on where I could go to learn? Or how they learned? Thanks in advance.

spacex
Posts: 637
Joined: 2015-01-17 01:27

Re: Bash Scripting

#2 Post by spacex »

How about starting here:
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/

or here: http://linuxconfig.org/bash-scripting-tutorial

There is no fun way to learn bash scripting. It becomes fun when you have learned the basics, but expect it to be a bit dry initially, as you don't get to do anything exciting with it until you know a little bit more.

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Bash Scripting

#3 Post by dasein »

DFC302 wrote:...alot of the tutorials are dry and not very helpful.
And so you thought you'd post here in hopes that someone would write yet another bash tutorial just for you, just to keep you entertained?

(I need new facepalms.)

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Bash Scripting

#4 Post by GarryRicketson »

I don't know much about actually writing bash scripts either,
Or how they learned?
I use the manual
and tutorials, and am still learning.
There are a lot of good tutorials, they may seem "dry",
but they are to instruct, not entertain.
Have you tried this:

Code: Select all

man bash
I made this little script, and it works,

Code: Select all

#!/bin/bash
#How to use and write bash scripts
man bash
echo RTFM 
one thing to note, if you have a file ,that is a bash script
it must have "executable" permissions, see

Code: Select all

man chmod
or simply select the file, select properties, check the "executable" box.
Here is another link to tutorial, I find it hard to believe you really did any searches, cause there
are tons of tutorials, but anyway,
http://linuxcommand.org/lc3_writing_shell_scripts.php
hope that helps, and welcome to the forum
Edited: there are also some other threads right here in the programming forum,
that have some examples, the most recent:
http://forums.debian.net/viewtopic.php?f=8&t=124033
After you actually try writing a real script, if it does not work, you could post it here,and maybe
some one can help explain why it does not work.
make sure you use the code boxes.
You need to know the linux commands, to really be able to write any kind of script.
However, you do need to have some idea about what the commands do, and understand what you read in the script, if you do not know what some command will actually do, you should look it up,
because it might be harmful to your system, fortunately the commands that effect the system, and could be harm full , also need to be run as root, you really need to be careful, in fact simply do not run any scripts the require root privileges, until you know what you are doing.
Last edited by GarryRicketson on 2015-08-09 01:30, edited 1 time in total.

DFC302
Posts: 4
Joined: 2015-08-07 19:13

Re: Bash Scripting

#5 Post by DFC302 »

Thanks guys for your help. Yeah I was probably being picky. I know I was actually. Thanks for all of the input and advice

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Bash Scripting

#6 Post by GarryRicketson »

Another thing, to run a bash script from the command line,
you need to use:

Code: Select all

./filename.sh
You can use any text editor to write the script, or copy and paste a script into a open
editer, then you save the file as a .sh file, any name you want. After setting the permissions
as executable, it can be run from the command line. It must be proceeded with the
" ./ " though.
http://www.cyberciti.biz/tips/spice-up- ... ripts.html
Has some interesting scripts.
I got this from there, and modified it a little.

Test-script.sh

Code: Select all

#!/bin/bash
 
# clear the screen
tput clear
 
# Move cursor to screen location X,Y (top left is 0,0)
tput cup 3 15
 
# Set a foreground colour using ANSI escape
tput setaf 3
echo "My Debian Wheezy"
tput sgr0
 
tput cup 5 17
# Set reverse video mode
tput rev
echo "M A I N - M E N U"
tput sgr0
 
tput cup 7 15
echo "1. User Management"
 
tput cup 8 15
echo "2. Service Management"
 
tput cup 9 15
echo "3. Process Management"
 
tput cup 10 15
echo "4. Backup"
 
# Set bold mode 
tput bold
tput cup 12 15
read -p "Enter your choice [1-4] " choice
 
tput clear
tput sgr0
tput rc 

So you would copy the text inside the code boxes, paste it into any text editor,
save the file as "Test-script.sh" make it executable, then at the command line;

Code: Select all

./Test-script.sh
to run.
Most of the time, one can find scripts, all ready written, for just about anything, and studying
those scripts, maybe modify them, so they do more exactly what you want, is a good way to
learn.

User avatar
mikejonesey
Posts: 69
Joined: 2011-06-22 13:05

Re: Bash Scripting

#7 Post by mikejonesey »

My personal website with blog n apps
http://www.mikejonesey.co.uk/

HarborView
Posts: 41
Joined: 2014-02-01 02:21
Location: The Space Coast of the USA

Re: Bash Scripting

#8 Post by HarborView »

My problem with this query was "Where is this guy coming from?"

I recently started programming in Bash without knowing the basics. I had something repetitive I wanted to automate.

But I know Redmond's .BAT system and Basic and Fortran and C and Pascal. Anyone looking at the programs could see the syntax is biased by experience in Basic.

It took a lot of cursing but I had an idea for what I was looking. Such as:

What's the syntax to concatenate strings? Is it & or + or some function I can imagine named concat or something. Search online for an example.

Oh. You just push them together.

Still didn't work with the literal I wanted in there. Quit for the day. Go to bed.

Don't know why It won't allow that literal. Put the literal in a variable. Works.

Like that. Now I'm doing the exercises in the Advanced Bash Scripting Guide.

My code is going from this:

Code: Select all

# K toggles between 1 and 0.
if (( K==0 )); then
  K=1
else
  K=0
fi

(Obviously replicating Basic since I couldn't find an xor command or function) to this:

Code: Select all

(( K==0 )) && K=1 || K=0

plumage
Posts: 7
Joined: 2015-07-23 15:55

Re: Bash Scripting

#9 Post by plumage »

This book might be what you're looking for:

http://linuxcommand.org/tlcl.php

seco
Posts: 5
Joined: 2017-02-02 17:08

Re: Bash Scripting

#10 Post by seco »

Hello,

This tutorial https://likegeeks.com/write-shell-script/ shows a lot about writing practical Bash scripts.
I remember the early days when I started learning Bash scripting! it was a bit tricky to understand (Everything on Linux is a file)

Post Reply