• Welcome to Bashguru

    Linux is one of popular version of UNIX operating System. It is open source as its source code is freely available. It is free to use. Linux was designed considering UNIX compatibility. It's functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide latest information about Linux Operating system.

  • Python Programming

    Python is a comparatively simple programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced with an overall easier to learn language with many “intuitive” features. For this reason it is common and recommended by most professionals that people new to programming start with python.

  • Perl Programming

    Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating easy manipulation of text files.

  • Android

    Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.

Sunday, November 15, 2009

Posted by venu k
127 comments | 1:19 AM

Like UNIX commands, shell scripts also accept arguments from the command line.
They can, therefore, run non interactively and be used with redirection and
pipelines.

Positional Parameters:

Arguments are passed from the command line into a shell program using the
positional parameters $1 through to $9. Each parameter corresponds to the
position of the argument on the command line.

The first argument is read by the shell into the parameter $1, The second
argument into $2, and so on. After $9, the arguments must be enclosed in
brackets, for example, ${10}, ${11}, ${12}.Some shells doesn't support this
method. In that case, to refer to parameters with numbers greater than 9, use
the shift command; this shifts the parameter list to the left. $1 is lost,while
$2 becomes $1, $3 becomes $2, and so on. The inaccessible tenth parameter
becomes $9 and can then be referred to.

Example:

#!/bin/bash
# Call this script with at least 3 parameters, for example
# sh scriptname 1 2 3

echo "first parameter is $1"

echo "Second parameter is $2"

echo "Third parameter is $3"

exit 0

Output:
[root@localhost ~]# sh parameters.sh 47 9 34

first parameter is 47

Second parameter is 9

Third parameter is 34

[root@localhost ~]# sh parameters.sh 4 8 3

first parameter is 4

Second parameter is 8

Third parameter is 3


In addition to these positional parameters, there are a few other special
parameters used by shell.Their significance is noted bellow.

$* - It stores the complete set of positional parameters as a single string.
$@ - Same as $*, But there is subtle difference when enclosed in double quotes.
$# - It is set to the number of arguments specified.This lets you design scripts
that check whether the right number of arguments have been entered.
$0 - Refers to the name of the script itself.

Setting Values of Positional Parameters

You can't technically call positional parameters as shell variables because all
variable names start with a letter. For instance you can't assign values to $1,
$2.. etc. $1=100 or $2=venu is simply not done. There is one more way to assign
values to the positional parameters, using the set command.

$ set Helping hands are holier than praying lips

The above command sets the value $1 with “Helping” , $2 with “hands” and so on.
To verify, use echo statement to display their values.

$ echo $1 $2 $3 $4 $5 $6 $7

Helping hands are holier than praying lips

You can simply use $* or $@

$ echo $*

Helping hands are holier than praying lips

$ echo $@

Helping hands are holier than praying lips

Using Shift on Positional Parameters

We have used set command to set upto 9 words. But we can use it for more.

$ set A friend who helps when one is in trouble is a real friend

$ echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11

A friend who helps when one is in trouble A0 A1


Observe that last two words in the output. These occurred in the output because
at a time we can access only 9 positional parameters. When we tried to refer to
$10 it was interpreted by the shell as if you wanted to out put the value of $1
and a 0. Hence we got A0 in the output.
Does that mean the words following the ninth word have been lost? No. If not,
then where have they gone?. They are very much there, safe with the shell But to
reach them we must do the following.

$shift 4

$ echo $1 $2 $3 $4 $5 $6 $7 $8 $9

when one is in trouble is a real friend


Now where have the first seven words gone? They have been shifted out.The first
four words lost for ever, as we did not take the precaution to store them else-
where. What should we have done is:

$ set A friend who helps when one is in trouble is a real friend

$ a=$1

$ b=$2

$ c=$3

$ d=$4

$ shift 4

$ echo $a $b $c $d $1 $2 $3 $4 $5 $6 $7 $8 $9

A friend who helps when one is in trouble is a real friend


Note:In the Korn and bash shells you can refer directly to arguments where
n is greater than 9 using braces. For example, to refer to the 57th positional
parameter, use the notation ${57}.some shells may not support this method.


$ set A friend who helps when one is in trouble is a real friend

$ echo ${12}

real

$ echo ${13}

friend


Bracket notation for positional parameters leads to a fairly simple way of
referencing the last argument passed to a script on the command line.

$ echo ${!#}

friend


$* and $@

Let us look at the subtle difference between $* and $@.

First create three files fileA, fileB, “file temp”
cat > fileA
I LOVE INDIA
Ctrl+d

cat > fileB
HELLO WORLD
Ctrl+d

cat > temp\ file
This file name contains blank space
Ctrl+d

Example:

#!/bin/bash
# Usage: sh arguments.sh fileA fileB temp\ file

echo -e "\033[1mDisplay files content using \$* \033[0m"

cat $*

echo -e "\033[1mDisplay files content using \$@ \033[0m"

cat $@

exit 0

Run the script with file names as arguments.

Output:
[root@localhost ~]# sh arguments.sh fileA fileB temp\ file
Display files content using $*
I LOVE INDIA
HELLO WORLD
cat: temp: No such file or directory
cat: file: No such file or directory
Display files content using $@
I LOVE INDIA
HELLO WORLD
cat: temp: No such file or directory
cat: file: No such file or directory

So there is no difference between cat $* and cat $@.

Modify arguments.sh script as


#!/bin/bash
# Usage: sh arguments.sh fileA fileB temp\ file

echo -e "\033[1mDisplay files content using \"\$*\" \033[0m"

cat "$*"

echo -e "\033[1mDisplay files content using \"\$@\" \033[0m"

cat "$@"

exit 0

Now again run the script with file names as arguments.

Output:
[root@localhost ~]# sh arguments.sh fileA fileB temp\ file
Display files content using "$*"
cat: fileA fileB temp file: No such file or directory
Display files content using "$@"
I LOVE INDIA
HELLO WORLD
This file name contain blank space

Now there is a difference, the two cat commands would become:

cat “fileA fileB temp file”
cat fileA fileB 'temp file'

On execution, the first of these commands would give an error since there does
not exist a file with the name “fileA fileB temp file”. As against this, the
second cat command would display the contents of the files fileA, fileB and
“temp file”. So what you observed ,when not enclosed within "double quotes"
$* and $@ behaves exactly similarly, and when enclosed in "double quotes" there
is a subtle difference.

127 comments:

  1. good job ..........i like ur way to explain

    ReplyDelete
  2. Very well explained!!! Thanks for sharing

    ReplyDelete
  3. what a way to explain, it;s excillent

    ReplyDelete
  4. Thanks, great tut.

    ReplyDelete
  5. very well explained Thanks!!!!!

    ReplyDelete
  6. thank you, it solved my issue.

    ReplyDelete
  7. Awesome work dude...

    ReplyDelete
  8. nice way of explaining............
    u r good teacher...
    i really liked it....
    regards sayali jadhav...

    ReplyDelete
  9. awesom! cleared my doubts!!

    ReplyDelete
  10. how about some switch related info to a script

    ReplyDelete
  11. I will try to post switch related scripts and info as soon as possible.

    ReplyDelete
  12. Excellent post man , truly useful just to add its worth noting that $* and $@ bash parameters behave identical without double quotes but entirely different if used within double quotes. in case of $* individual parameters will be separated by IFS characters while in case of $@ individual parameters will appear in quotes and separated by space.

    Thanks
    Javin
    10 example of grep command in Unix

    ReplyDelete
  13. Explained very well..Thanks Man

    ReplyDelete
  14. Thanks for sharing your knowledge.
    Really very good explanation.

    ReplyDelete
  15. Thank you very much. I got my solution here.

    ReplyDelete
  16. very helpful ..you have done a great job

    ReplyDelete
  17. Amazing way of explaining!!!!!!!

    ReplyDelete
  18. Really Good Explanation -- Vikas Shah

    ReplyDelete
  19. superb!!!!...thanks

    ReplyDelete
  20. Excellent Explanation.. Thanks

    ReplyDelete
  21. way better than my professor, gosh, don't know why I should pay him

    ReplyDelete
  22. thanks. it helps a lot

    ReplyDelete
  23. thanx a lot..
    understood everything very well.
    keep up the good work.

    ReplyDelete
  24. nice post,it really helped me
    regards aamir

    ReplyDelete
  25. Finally! I've been looking for how to set command line args for days! Thank-you!

    ReplyDelete
  26. how to pass parameter to shell script run by scheduled job

    BEGIN
    sys.dbms_scheduler.create_job(
    job_name => '"SYS"."RUN_UPDATE_UCM"',
    job_type => 'EXECUTABLE',
    job_action => '/home/oracle/bkp_script/ucm.sh',
    start_date => systimestamp at time zone 'Asia/Karachi',
    job_class => 'DEFAULT_JOB_CLASS',
    auto_drop => FALSE,
    number_of_arguments => 1,
    enabled => FALSE);
    sys.dbms_scheduler.set_job_argument_value( job_name => '"SYS"."RUN_UPDATE_UCM"', argument_position => 1, argument_value => '/home/oracle/bkp_script/ucm.sh');
    sys.dbms_scheduler.enable('"SYS"."RUN_UPDATE_UCM"');
    END;

    we want to pass date parameter to ucm.sh script as we do in OS

    # ./ucm.sh 20jun2013

    which is run successfully...

    ucm.sh script code is following:

    #!/bin/bash
    export ORACLE_HOME=/u01/app/oracle/middleware/asinst_1/config/reports
    export ORACLE_SID=scm
    $ORACLE_HOME/bin/rwrun.sh report=/home/ERP/SOFTWARES/UCM_SUMMARY_std.RDF userid=scm/palchandarysm@scm desformat=pdf DESTYPE=file DESNAME=/home/ERP/SOFTWARES/PDF/ucm_summary.pdf P_REPORT_DATE=$1

    now we want to run this script from pl/sql and want to pass date parameter as:

    BEGIN
    DBMS_SCHEDULER.run_job('RUN_UPDATE_UCM');
    END;

    ReplyDelete
  27. plz send me reply at this address

    nazar.yousafzai@gmail.com

    ReplyDelete
  28. very thanks.............

    ReplyDelete
  29. Good morning is good explanation. Some one can helpme? I have a problem, is the next:

    my script is call :

    sh mysScript.sh param1 param2 param3

    par1=$1
    par2=$2
    par3=$3

    sudo mkdir $par1/$par2/$par3

    it cannot create the file because the route is generated like this:
    parameter1 /parameter2 /parameter3

    with a blank space.. what is my wrong?

    ReplyDelete
    Replies
    1. Mr. Anonymous,

      One way of doing this is

      root@reachvikas.com:~# par1="a "; par2="b "; par3="c "
      root@reachvikas.com:~# mkdir -p /`echo $par1 | sed 's/ //'`/`echo $par2 | sed 's/ //'`/`echo $par3 | sed 's/ //'`
      root@reachvikas.com:~# ls -ld /a/b/c
      drwxr-xr-x 2 root root 4096 Dec 28 02:42 /a/b/c
      root@reachvikas.com:~#

      Regards,
      Vikas

      Delete
  30. Hi i am using this script
    cat pfile.sh
    #!/usr/bin/ksh

    echo $* >test.txt

    I am executing this

    ./pfile.sh a b c d e f g h i j k

    and my output is a b c d e f g h i j k

    now i want the same script to work in such a way that my each parameter comes in a new line

    I dont know the number of parameters that i will be passing so i cannot usis this

    echo -e "$2\n$3\n$4\n">test.txt

    ReplyDelete
  31. Mr. Anonymous,

    One way of doing this is ./pfile.sh a b c d e f g h i j k | tr " " "\n"

    Regards,
    Vikas

    ReplyDelete
  32. Well post and this article tell us how to solve shell script in math and how pass math exams thanks for sharing how to write a statement of purpose for mba .

    ReplyDelete
  33. Very Good way to explain very nice

    ReplyDelete
  34. If you don’t remember the rules of citation, student will only use points instead of earning them, have a glimpse to find more!

    ReplyDelete
  35. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog. Jasa Seo Murah Terbaik Terpercaya antara Jasa Seo

    ReplyDelete
  36. i created one shell script and passing the argument while executing time. plz help me to echo those value in same script.

    ./setup.sh --key=value

    ReplyDelete
  37. Good . i tried it.
    Chase4Net is a reputed software training Institute for CCNA Training in Marathahalli,JAVA Training in Marathahalli,Python Training in Marathahalli,Android Training in Marathahalli .
    Our main focus areas are Java Certification Training and Software Development.Chase4Net has been already ranked as the No.1 for CCNA Training in Bangalore,JAVA Course in Bangalore,Python Training in Bangalore,Android Training in Bangalore.
    We provide quality education to the students at low cost.Students will get real experience.We are the Best Java Certification Training institute in bangalore.

    ReplyDelete
  38. The share your really gives us excitement. Thanks for your sharing. If you feel tired at work or study try to participate in our games to bring the most exciting feeling. Thank you!
    wingsio game | super mechs 2 | wingsio | super mechs

    ReplyDelete
  39. The blog gave me idea to Pass Arguments to Shell Script my sincere thanks for sharing this post please continue to share this post
    Python Training in BTM Layout

    ReplyDelete
  40. Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.

    ReplyDelete
  41. Mathematics is too complicated for me, I ordered all my work on an essays service and I'm quite happy that I did not have to learn it myself.

    ReplyDelete
  42. Now I know that I was reading a blog with a post that is very commete. Thank you for sharing the information you post. I just subscribe to your blog. This is a great blog.

    ReplyDelete
  43. Very good information with excellent explanation. Thanks so much for sharing such a wonderful article. Austere Technologies| Best Cloud Solution Services

    ReplyDelete
  44. Awesome explantion and massiltechnologies also providing best mobile application development Mobile application development Services

    ReplyDelete
  45. Great Explanation......Very Easy to Understand the Concept....
    Thanks@Salesforce Developer Training

    ReplyDelete
  46. Interesting information with good explanation. Thanks for sharing.

    Best Mobility Services | Austere Technologies

    ReplyDelete
  47. wow...Excellent blog, very helpful information. Thanks for sharing.

    Best IT Security Services | Austere Technologies

    ReplyDelete

  48. Wow!! What a interesting blog.. Really awesome to read and so informative blog.. Keep ongoing such a great blog..
    Internet Of Things

    ReplyDelete
  49. Tahun 1957, Edam kembali digunakan tuk menampung orang jompo, namun sekali lagi aktivitas inipun berlalu tanpa hasil apapun, juga Pulau Edam kembali jadi pulau sepi yang jadi saksi perjalanan waktu. Juga utk lebih didalam mengetahui histori didalam zaman penjajahan Belanda ( VOC ), para traveler di ajak keliling delapan pulau kira-kira yang terdapat peninggalan peninggalan histori zaman Belanda, antaranya pulau Onrust, pulau kelor juga pulau kayangan. lihat disini sekarang Utk urusan Penginapan di pulau Tidung Agan tak wajib risau, pulau tidung dilengkapi dengan kedua Kategori fasilitas Cottage, yaitu fasilitas penginapan pinggir pantai juga fasilitas home stay (villa), yang seluruhnya telah dilengkapi dengan fasilitas yang benar-benar baik utk ukuran homestay.

    Para penjaga penangkaran hiu bisa menjamin keselamatan para turis kalau ada sesuatu yang muncul di luar kendali. Jika Anda menyukai artikel ini dan Anda ingin menerima informasi lebih lanjut tentang kunjungi halaman ini menjamin kunjungi website. Berbagai spesies ikan seperti gobes (gobiidae), damselfishes (pomacentridae), wrasses (labridae), ikn pari biru (manta rays) yang mempunyai besar mencapai 3,6 - 7 m, luba-lumba, paus juga beraga spesies ikan juga terumbu karang menarik lain dapat Agan saksikan kecantikannya secara langsung. Here didalam after referd as PASSPORT wil be issued uppon receaving the full payment dari the Guest. lihat website ini Di kesempatan yang tidak berbeda, dibentuk juga Badan Otorita Danau Toba. Ongkos yang saya tawarkan serta sesuai hingga agan tepatnya tak nantinya kecewa dengan layanan yang saya beri.

    ReplyDelete
  50. This comment has been removed by the author.

    ReplyDelete
  51. Thanks for sharing this informative post.
    I can suggest you some really reliable writing service to deal with your assignment. Our advanced writers will help you with any your assignment.

    ReplyDelete
  52. Excellent information you made in this blog, very helpful information. Thanks for sharing.

    chartered accountant | Avinash college of commerce

    ReplyDelete
  53. Very interesting about this article and I'm alreay make wirh this Code. Thanks so much very helpful

    ReplyDelete
  54. Great article, really very helpful content you made. Thank you, keep sharing.

    Best Degree Colleges Hyderabad | Avinash College of Commerce

    ReplyDelete
  55. Thank you for sharing this valuable information. But get out of this busy life and find some peace with a beautiful trip book best Andaman honeymoon packages

    ReplyDelete
  56. Get the best AWS Training in Bangalore Classroom or Live Online.Get started to become hands on experts on AWS . that have taken AWS Training at myTectra the Market Leader on AWS Training in Bangalore.

    ReplyDelete
  57. You can buy dissertations and save more time for your personal hobbies.

    ReplyDelete
  58. Hi Thanks for the nice information its very useful to read your blog. We provide best Finance Training in Hyderabad

    ReplyDelete
  59. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    Python training in Bangalore
    Python online training
    Python Training in Chennai

    ReplyDelete
  60. Thank you for sharing this valuable information. But get out this busy life and find some peace with a beautiful trip. book ANDAMAN HOLIDAY PACKAGES @ 35999

    ReplyDelete
  61. Thank you for sharing this valuable information. But get out this busy life and find some peace with a beautiful trip. book CHEAP ANDAMAN PACKAGES @9999

    ReplyDelete
  62. This comment has been removed by the author.

    ReplyDelete
  63. Hi Thanks for the nice information its very useful to read your blog. We provide best Find All Isfs Courses

    ReplyDelete
  64. It was very informative, thanks for sharing the good post start your career with DevOps DevOps Training in Bangalore

    ReplyDelete
  65. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. 

    angularjs Training in chennai
    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    ReplyDelete
  66. Its an incredible joy perusing your post. It's brimming with data I am searching for and I want to post a remark that "The substance of your post is marvelous" Great work.
    Angularjs Courses in Chennai
    Angularjs Training in Chennai

    ReplyDelete
  67. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    python training Course in chennai | python training in Bangalore | Python training institute in kalyan nagar

    ReplyDelete
  68. myTectra offers DevOps Training in Bangalore,Chennai, Pune using Class Room. myTectra offers Live ">Online DevOps Training Globally

    ReplyDelete
  69. Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work

    DevOps is currently a popular model currently organizations all over the world moving towards to it. Your post gave a clear idea about knowing the DevOps model and its importance.

    Good to learn about DevOps at this time.

    devops training in chennai | devops training in chennai with placement | devops training in chennai omr | devops training in velachery | devops training in chennai tambaram | devops institutes in chennai

    ReplyDelete
  70. Excellent tutorial buddy. Directly I saw your blog and way of teaching was perfect, Waiting for your next tutorial.
    best rpa training institute in chennai | rpa training in velachery | rpa training in chennai omr

    ReplyDelete
  71. Thank for sharing very valuable information.nice article.keep on blogging.For more information visit
    aws online training
    aws training in hyderabad
    aws online training in hyderabad

    ReplyDelete
  72. Hey, wow all the posts are very informative for the people who visit this site. Good work! We also have a Website. Please feel free to visit our site. Thank you for sharing.
    Well written article.Thank You Sharing with Us .android device manager location history

    ReplyDelete
  73. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    datapower interview questions

    ReplyDelete

  74. I think things like this are really interesting. I absolutely love to find unique places like this. It really looks super creepy though!!Roles and reponsibilities of hadoop developer | hadoop developer skills Set | hadoop training course fees in chennai | Hadoop Training in Chennai Omr

    ReplyDelete
  75. Thanks for your great and helpful presentation I like your good service.I always appreciate your post. That is very interesting I love reading and I am always searching for informative information like this. Well written article ------- Thank You for Sharing with Us angular 7 training in chennai | angular 7 training in velachery | Best angular training institute in chennai

    ReplyDelete
  76. Flexera Global can offer you an effective and cost-effective full-range of MuleSoft services including: explore us at Enterprise application integration Solutions

    ReplyDelete
  77. This information is impressive. I am inspired with your post writing style & how continuously you describe this topic. Eagerly waiting for your new blog keep doing more.
    Ethical Hacking Training in Bangalore
    Ethical Hacking Course in Bangalore
    Advanced Java Course in Bangalore
    Java Coaching Institutes in Bangalore
    Advanced Java Training Institute in Bangalore

    ReplyDelete
  78. Amazing Post . Thanks for sharing. Your style of writing is very unique. Pls keep on updating.

    Article submission sites
    Technology

    ReplyDelete
  79. Good job!you were given an interesting and innovative information's. I like the way of expressing your ideas and i assure that it will make the readers more enjoyable while reading.
    Selenium Training in Saidapet
    Selenium Training in Ashok Nagar
    Selenium Training in Navalur
    Selenium Training in Karapakkam

    ReplyDelete