LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-28-2012, 02:05 AM   #1
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Rep: Reputation: Disabled
Unhappy What is the meaning of $# in shell scripts?


Hi everyone,
What is the meaning of the below line in scripting
if [ $# -ne 1 ]
 
Old 03-28-2012, 02:07 AM   #2
xiaokun
LQ Newbie
 
Registered: Mar 2012
Location: Hangzhou
Posts: 3

Rep: Reputation: Disabled
$# means arguments' number . -ne means !=
 
Old 03-28-2012, 02:09 AM   #3
xiaokun
LQ Newbie
 
Registered: Mar 2012
Location: Hangzhou
Posts: 3

Rep: Reputation: Disabled
FYI
$0 script name
$1 first argument
$2 second ..
$@ all argument
$* all arguments in one string
$$ current pid
$? exit status
 
Old 03-28-2012, 02:09 AM   #4
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
Hey xiaokun,
Arguments???I did not get you correctly
 
Old 03-28-2012, 02:10 AM   #5
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
okok.
can you suggest me one website for preparing scripting........
 
Old 03-28-2012, 02:18 AM   #6
xiaokun
LQ Newbie
 
Registered: Mar 2012
Location: Hangzhou
Posts: 3

Rep: Reputation: Disabled
For example, in script hello.sh

#!/bin/bash
echo $#
echo "hello world"

when u run as ./hello.sh 0 1 2
it will show:
3
hello world

website.... u should google it
 
Old 03-28-2012, 02:38 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by ravi_nandula View Post
okok.
can you suggest me one website for preparing scripting........
I'd suggest a really good reading on the Linux command line in general (shell scripting included): http://linuxcommand.org/tlcl.php (check the on-line version). Another good resource is the Linux Documentation Project http://www.tldp.org (check the longer in-depth guides, e.g. The Bash Guide for Beginners). Hope this helps.
 
Old 03-28-2012, 03:06 AM   #8
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
thank you Colucix...
 
Old 03-28-2012, 03:10 AM   #9
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
hey ,

I need a script that prints name of person...by taking it as string.....say s1 n s2 and will finally print the full name i.e,. (s1 + s2).
s1 - raj
s2 - sekhar
.....I need output as " raj sekhar".....

please help mee............
----------------------------------
do we have strings in scripting?????????
 
Old 03-28-2012, 03:20 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Input from the user is acquired by the read statement. Simple printing out is performed by echo (properly use double quotes to put a space between the two strings).

At this point I strongly suggest to start reading the aforementioned book. You will learn a lot and if you're stuck at some point feel free to ask. Always post what have you tried and give as many details as possible on error messages (if any) or unexpected behaviour of your code, specifying the input and the desired output. Using CODE tags is also advisable to improve readability of the posted code and preserve spacing and indentation.

To use CODE tags, either put [CODE] and [/CODE] around the text or switch to Advanced Mode (see the Go Advanced button at the bottom of the Quick Reply box), select the text and press the # button. The CODE tags will be added automatically.

Finally, please don't post multiple questions in the same thread: the discussion is difficult to follow and your new questions will not have the visibility they deserve. Thank you.
 
Old 03-28-2012, 04:19 AM   #11
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
Quote:
Originally Posted by ravi_nandula View Post
hey ,

I need a script that prints name of person...by taking it as string.....say s1 n s2 and will finally print the full name i.e,. (s1 + s2).
s1 - raj
s2 - sekhar
.....I need output as " raj sekhar".....

please help mee............
----------------------------------
do we have strings in scripting?????????
Code:
read -p "First Name: " F_NAME
read -p "Last Name: " L_NAME
echo "Welcome, $F_NAME $L_NAME"
Save it as example.sh

and then do this to set the execution bit on:

Code:
chmod 755 example.sh
and then this to run the script:

Code:
./example.sh
Add this line at the top of the script file (recommended and is the best practice):
Code:
#!/bin/bash
check the correct path on your system by executing this command:

Code:
which bash
.

Last edited by devUnix; 03-28-2012 at 04:22 AM.
 
Old 03-28-2012, 04:25 AM   #12
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
Quote:
Originally Posted by ravi_nandula View Post
Hey xiaokun,
Arguments???I did not get you correctly
Whatever you specify after a script's/program's name is called Command-Line Argument. Examples:

Code:
ls fileName
file fileName
./myScript.sh arg1 arg2 argn
In your script you will access arg1 by saying $1 and so on up to $9. Refer to the bash guide on www.tldp.org for more details.
 
Old 03-28-2012, 04:34 AM   #13
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by devUnix View Post
Whatever you specify after a script's/program's name is called Command-Line Argument. Examples:

Code:
ls fileName
file fileName
./myScript.sh arg1 arg2 argn
In your script you will access arg1 by saying $1 and so on up to $9. Refer to the bash guide on www.tldp.org for more details.
yah.Now I got your point.
 
Old 03-28-2012, 04:35 AM   #14
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
hey I need one more script
That will print the wishes like gud morning,eveng n night.....whenever system gets booted.......?
 
Old 03-28-2012, 05:16 AM   #15
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
Quote:
Originally Posted by ravi_nandula View Post
hey I need one more script
That will print the wishes like gud morning,eveng n night.....whenever system gets booted.......?
Ravi, you need bash Beginner's Guide or Advanced Bash Scripting Guide found on www.tldp.org

To run anything at log-in, edit your .bash_profile file found your home directory (the one that you are placed in when you log-in to your Unix/Linux system). Check it by issuing this command:

Code:
ls -ld .*
or


Code:
ls -la
You should see files ending in "profile" beginning with ".". It could be .profile or .bash_profile or .sh_profile. Usually, it is your "profile" file that is executed whenever your log-in to your system. Similarly there is a .logout file whose function should be obvious now.

So, add this line at the end of your .profile file:

Code:
echo "Hello, ${USER}!"


and add these lines at the end of your .logout file:

Code:
echo "Bye, ${USER}!"
read -p "Press enter to log out..."
You can actually do more than that. Just keep your codes in a separate script file and then call it from within any of the above files:


Code:
./myScript.sh
A Note on the Variable: We enclose the variable's name with curly braces in case any other character or word is adjacent to it so that it is separately taken as a variable and not as a part of the other word. In the above case, we have separated the variable's name from the exclamation mark.

To check your home directory issue this command at the Command Prompt / Shell Prompt:

Code:
echo $HOME
To directly get into your home directory, do this:

Code:
cd
Yes, no argument is supplied this time. $HOME is taken by default.

Last edited by devUnix; 03-28-2012 at 05:18 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] what is the meaning $$ in shell procfs Linux - Newbie 3 01-13-2011 11:25 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
meaning of if [ -z $SOMETHING....] in bash shell darwinianlo Linux - Newbie 8 08-09-2007 11:02 AM
what is the meaning of editor & scripts Fuzia Linux - Newbie 5 04-06-2006 07:02 AM
'./file' meaning in python & shell script Chowroc Programming 7 12-30-2005 02:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration