JoseManimala/Notes

From FedoraProject

Jump to: navigation, search

Shell Script Basics

  • Most of us know how to write shell scripts, but when i was a Newbie i had a lot of trouble trying to figure out how to write a script that can do more than just printing my name on screen.
  • Lets look at the basics
  • what is a shell script?

A shell is an access point to your operating system and kernel(heart of the OS!!). Any command that is typed in this screen is a shell command. Now a shell script is a set of shell commands put into a file so that they can be run together. This process is called batch processing.

  • Creating a shell script:

Use any editor you have. Save the file as anything you want but the standard method is to save the file as <filename>.sh for shell-script. Now type the commands in making sure that only one command comes on one line.

  • Executing a shell script:

To execute a shell script you can use the $exec <filename>.sh command or use the ./<filename>.sh command. If it says execute permission denied, you have to change the permissions. just type chmod u+x <filename>.sh. The command means allow user(u) to(+) execute(x)<filename>.sh

  • Variables in shell scripts:

Using variables in shell scripts can be a little tricky. But here is a simple way $n = 10 stores the value 10 to n. $n = n+1 increments the value of n. Its very simple to do. $~ echo $n will print the value of n. storing string to variable is easy $ name='HELLO'.

  • Basic shell commands:
  • echo - print something on screen
  • read - to read from screen
  • expn 2+3 - will add two and three