LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-29-2013, 09:07 AM   #1
kingissingh@gmail.com
LQ Newbie
 
Registered: May 2013
Distribution: Ubuntu,Fedora,CentOS,Solaris 10, RHEL
Posts: 6

Rep: Reputation: Disabled
Cool Use of $ in shell script


I have the following script that counts the number of A/a entered by user. But I do not completely understand the line 4 and 6. I will be thankful if some one explains what is the special use of $ here.

1. #!/bin/bash -
2. echo "Enter A or a"
3. read text
4. A="${text//[^a|A]}"
5. echo "$A"
6. echo "${#A}"

O/P
------------
$ ./test_move
Enter A or a
AAALL
AAA
3
 
Old 05-29-2013, 09:23 AM   #2
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
http://www.gnu.org/software/bash/man...Expansion.html

pattern substitution (4) and string length (6)
 
1 members found this post helpful.
Old 05-29-2013, 11:13 AM   #3
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 506

Rep: Reputation: 139Reputation: 139
Pattern search and replace:

There are 4 type of pattern search and replaces,
  1. ${PARAMETER/PATTERN/STRING} = In the given PARAMETER, the first PATTERN is matched and is replaced by STRING
  2. ${PARAMETER//PATTERN/STRING} = In the given PARAMETER, all the PATTERN(s) are matched and are replaced by STRING
  3. ${PARAMETER/PATTERN} = In the given PARAMETER, the first PATTERN is matched and is replaced by nullstring, i.e. nothing
  4. ${PARAMETER//PATTERN} = In the given PARAMETER, all the PATTERNS(s) are matched and are replaced by nullstring, i.e. nothing

In your case its type 4:

let's say you input text="A a B X a AAaa"

here PARAMATER is "A a B X a AAaa" and the PATTERN that is provided is [^a|A] i.e, not a or not A

"${text//[^a|A]}" means replace all which are not either a or A in text with nullstring. so the space and the letters other than a or A will be removed. the result will be AaaAAaa

"${#A}" means length of the string in variable A. result will be 7

I hope i have made it clear.

more: Parameter expansion

Last edited by mddnix; 05-29-2013 at 11:42 AM.
 
1 members found this post helpful.
Old 05-30-2013, 04:56 AM   #4
kingissingh@gmail.com
LQ Newbie
 
Registered: May 2013
Distribution: Ubuntu,Fedora,CentOS,Solaris 10, RHEL
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thumbs up Thank you

That was a good explanation from mddesai, also thank for the links posted by mddesai and Ygrex.
The links help me get more information that I was looking for, appreciate it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell script for run an shell script on server using ssh bloodstreetboy Linux - Server 5 01-12-2013 04:23 AM
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 10:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM


All times are GMT -5. The time now is 06:28 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration