LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   what is shell scripting? (https://www.linuxquestions.org/questions/linux-newbie-8/what-is-shell-scripting-862802/)

sujitkulkarni 02-15-2011 05:37 AM

what is shell scripting?
 
What is shell scripting? what is 'bash'? :study:

EricTRA 02-15-2011 05:54 AM

Hello,

Asked and answered a million times before. Please put in some work and Google, there are so many documents online that go into detail on what shell scripting and Bash is.

Kind regards,

Eric

ozanbaba 02-15-2011 05:56 AM

It's God's judgment on humanity. And use a web search like DuckDuckGo.

Aquarius_Girl 02-15-2011 06:17 AM

BASH: Bourne Again shell

Bash is a type of shell in Unix/Linux.

A shell in Linux/Unix means something which takes as input the user specified
commands, executes them and gives us the output.

Every command recognized by the shell is actually a program whose executable is
stored in the folders like /bin.

Example:
ls is a command which lists all the files in a folder.
So here we are using ls command to list out all the folders in the
root directory. (root directory is represented by / in bash,
so when we say, /bin, it means the bin directory in the root folder)

Code:

anisha@linux-uitj:~> ls /
bin  etc        initrd.lz  media  proc  selinux  tmp  vmlinuz
boot  home        lib        mnt    root  srv      usr
dev  initrd.img  lost+found  opt    sbin  sys      var

All the files in the below output are recognized by the shell as valid executables.
Example:
In the below example notice the ls command itself listed inside the /bin
folder as an executable.
Code:

anisha@linux-uitj:~> cd /bin
anisha@linux-uitj:/bin> ls
arch                  df            gzip        mkdir          pwd              sync
awk                  dmesg          hostname    mknod          readlink        tar
csh                  fgconsole      loadkeys    pidof          setserial        usleep
date                  fgrep          loadunimap  ping          sh              vi
dbus-cleanup-sockets  fillup        logger      ping6          showconsolefont  vim
dbus-daemon          fsync          login        pkill          showkey          vim-normal
dbus-monitor          fuser          ls          ps            sleep            ypdomainname
dbus-send            gawk          lsmod        psfaddtable    sort            zcat
deallocvt            gunzip        md5sum      psfxtable      su

If the shell recognizes the commands, it executes its executable file.
The commands recognized by the shell usually call dozens of other functions dealing
directly with the Linux kernel.

It looks too simple??, to know which functions does ls call "internally" to get the list of the files, there is another command named 'strace':

The titles which you see in red colour below are actually the functions called by ls, internally.
Code:

anisha@linux-uitj:~> strace ls                                               
execve("/bin/ls", ["ls"], [/* 89 vars */]) = 0                                         
brk(0)                                  = 0x618000                                     
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa7724ef000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa7724ee000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)         
open("/usr/lib64/mpi/gcc/openmpi/lib64/tls/x86_64/librt.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)                                                                                                 
stat("/usr/lib64/mpi/gcc/openmpi/lib64/tls/x86_64", 0x7fff75b561b0) = -1 ENOENT (No such file or directory)                                                                                                     
open("/usr/lib64/mpi/gcc/openmpi/lib64/tls/librt.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
...                                                                                 
write(1, "alltray-0.7.4dev.tar.gz\t\t\t opera"..., 55alltray-0.7.4dev.tar.gz                    opera-10.62-6438.x86_64.rpm                                                                                     
) = 55                                                                                                 
write(1, "aptoncd\t\t\t\t\t osm2pgsql\n", 23aptoncd                                      osm2pgsql     
) = 23                                                                                          ...       
anisha@linux-uitj:~>

and finally check out the following:
1. http://tldp.org/LDP/Bash-Beginners-Guide/html/
2. http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
3. http://tldp.org/LDP/abs/html/

honeybadger 02-15-2011 07:14 AM

@Anisha: Do you think the OP even can _read_ the script. Heck, I gave up after the second line. I have however bookmarked the page and will read this later.
@OP: Bash scripting is something *nix folks do. It is actually used to scare newbies like you. Hang in there, you would eventually understand.

Aquarius_Girl 02-15-2011 07:18 AM

Quote:

Originally Posted by SilverBack (Post 4259031)
@Anisha: Do you think the OP even can _read_ the script.

What script? I only explained him the meaning of bash.

Quote:

Originally Posted by SilverBack (Post 4259031)
@OP: Bash scripting is something *nix folks do. It is actually used to scare newbies like you. Hang in there, you would eventually understand.

and writing statements like these is NOT helpful specially when you are talking to a newbie!

EricTRA 02-15-2011 07:40 AM

Quote:

Originally Posted by SilverBack (Post 4259031)
@Anisha: Do you think the OP even can _read_ the script. Heck, I gave up after the second line. I have however bookmarked the page and will read this later.
@OP: Bash scripting is something *nix folks do. It is actually used to scare newbies like you. Hang in there, you would eventually understand.

Hello,

Sorry to disagree with you but what Anisha posted was in my opinion a very clear and more then complete explanation of what Bash is, and all of that in easy to understand terms. Heck, it's a lot more then what I would post to explain something that's basic knowledge and easy to find on the internet (hence my previous post).

Making a statement like you do, bringing down the importance of Bash (and for what it's worth other shells), gives a very wrong image of Linux/Unix. How is OP to understand like you point out he eventually will, when you 'bash' a perfectly good basic explanation?

Kind regards,

Eric

hughetorrance 02-15-2011 07:45 AM

kernel and shell
 
Quote:

Originally Posted by sujitkulkarni (Post 4258955)
What is shell scripting? what is 'bash'? :study:

Asking that is like asking how long is a piece of string...as like in a nut (hazel nut,walnut,etc etc)the kernel is surrounded by the shell...

http://www.freeos.com/guides/lsst/ :)good luck:)

Google "shell scripting" and "bash" without the parentheses

Aquarius_Girl 02-15-2011 07:50 AM

Eric, thank you, for the soothing words you wrote as a comment in the reputation column. I enjoyed them so much that I was forced to make this post, just to thank you.

EricTRA 02-15-2011 07:54 AM

Hi Anisha,

No thanks necessary, in my opinion you did a great job with that post in very understandable words. Should be appreciated by all newbies looking for a simple explanation of something that can go to very high complexity. Thank you for taking the time to post it. I'm sure a lot of users will agree with me. Statements like SilverBack's one don't add to the valor of Linux at all.

Kind regards,

Eric

citi 02-15-2011 08:08 AM

Document
 
1 Attachment(s)
here is the pdf "Introduction to bash scripting" this should give you a good foundation along with Anisha's examples

EricTRA 02-15-2011 08:10 AM

Hi citi,

Very nice, thanks for sharing it. I'm sure it'll be of use to someone.

Kind regards,

Eric

MTK358 02-15-2011 08:14 AM

But it still seems very odd and inappropriate to introduce a newbie to bash using strace...

Anyway, bash is the program that interprets the commands you type at a terminal, and a shell script is a file full of commands that can be run by the shell so you don't have to type them one by one.

Like Anisha said, commands are actaully executable programs in certain directories. But some are built into the shell.

The shell also has syntax for variables, if-then-else statements, wildcards, math, etc.

EricTRA 02-15-2011 08:17 AM

Quote:

Originally Posted by MTK358 (Post 4259093)
But is still seems very odd to introduce a newbie to bash using strace...

Hi,

I thought it was a nice touch using strace to show that it's not all that simple as just using ls but that there's a 'world' of complex things below what we use on a regular basis. Something about what most newbies don't even think about but can be very useful information. If you want to tame the beast it's best to understand it's nature :)

Kind regards,

Eric

citi 02-15-2011 08:34 AM

Your welcome Eric, just trying to help in any way possible


All times are GMT -5. The time now is 11:48 PM.