LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-23-2011, 01:49 PM   #1
kenny53067
Member
 
Registered: Jul 2011
Location: Rio Grande City, Texas
Posts: 54

Rep: Reputation: Disabled
Unhappy writing a script


how do I write a workable script that demonstrates that variable expansion occurs before pathname expansion. Can someone help me, I don't know where or how to start.
 
Old 07-23-2011, 02:19 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Sounds suspiciously like homework. Have you tried googling for any of this? If it is in fact homework one would think you have probably already been instructed and so it is
in notes somewhere.
 
Old 07-23-2011, 02:37 PM   #3
kenny53067
Member
 
Registered: Jul 2011
Location: Rio Grande City, Texas
Posts: 54

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Sounds suspiciously like homework. Have you tried googling for any of this? If it is in fact homework one would think you have probably already been instructed and so it is
in notes somewhere.
Our instructor gave us this:
$ ls g?

g1 g2 g5 g8

$ a=g?

$ echo "$a"

g?

$ echo $a

g1 g2 g5 g8

but I don't understand what this is.
 
Old 07-23-2011, 02:47 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well in your example 'a' is a variable and '?' is being used by the shell to expand into any single character. So I believe this gives you exactly the information you require to answer your question.
 
1 members found this post helpful.
Old 07-23-2011, 02:51 PM   #5
kenny53067
Member
 
Registered: Jul 2011
Location: Rio Grande City, Texas
Posts: 54

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Well in your example 'a' is a variable and '?' is being used by the shell to expand into any single character. So I believe this gives you exactly the information you require to answer your question.
so without the question mark g would not expand, and this would cause only g8 to show in the output. Is this correct?
 
Old 07-23-2011, 04:12 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Think about what you are saying, would the following work with your logic:
Code:
$ ls g
g8
 
Old 07-23-2011, 04:13 PM   #7
kenny53067
Member
 
Registered: Jul 2011
Location: Rio Grande City, Texas
Posts: 54

Original Poster
Rep: Reputation: Disabled
Question

Quote:
Originally Posted by kenny53067 View Post
so without the question mark g would not expand, and this would cause only g8 to show in the output. Is this correct?
$ a=g? Is this an example of variable expansion?

$ echo "$a" Is an example of pathname expansion?

Am I understanding this correctly?
 
Old 07-23-2011, 04:25 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Am I understanding this correctly?
ummm ... not exactly

Code:
$ a=g?
This is an example of variable assignment. Variable expansion is not performed until the variable prefixed with a $
Code:
$ echo "$a"
This is an example of a command acting on a variable after expansion
 
Old 07-23-2011, 04:28 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Shell operations, including their order is explained here. "Not a lot of people know that"! But it is key to a full understanding of how bash works. Time for bed now; will answer your specific question -- if not answered already -- tomorrow,
 
Old 07-23-2011, 04:30 PM   #10
kenny53067
Member
 
Registered: Jul 2011
Location: Rio Grande City, Texas
Posts: 54

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Think about what you are saying, would the following work with your logic:
Code:
$ ls g
g8
$ ls g This command would list all files that contain g Is this correct?
 
Old 07-23-2011, 04:42 PM   #11
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello kenny53067,

you should write some code by yourself and try it out. Learning programming doesn't work without experience. Just work in it.

Note that it is much easier for the advanced members here at LQ to help you with some code you've written.

Markus
 
Old 07-23-2011, 04:48 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
$ ls g This command would list all files that contain g Is this correct?
markush's response is the correct one here. Try it for yourself and see what happens. Create three files starting with the letter g and see what
happens when you issue the command.
 
Old 07-23-2011, 04:54 PM   #13
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by kenny53067 View Post
$ ls g This command would list all files that contain g Is this correct?
No. Actually that command will only list a file or directory named "g", if exists. You need to use wildcards in your command so the shell expands them.

Have a look at the bash manpage, section EXPANSION. You'll find there the types of expansion the shell performs, and their explanation.
 
Old 07-23-2011, 05:00 PM   #14
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello,

@kenny53067, it seems you're posting from a Windows-computer. If you don't have Linux running, you should either boot a live-CD or use for example Virtualbox and install a Linux-distribution as a virtual machine. Then you can try out the code.

Markus
 
Old 07-23-2011, 05:06 PM   #15
kenny53067
Member
 
Registered: Jul 2011
Location: Rio Grande City, Texas
Posts: 54

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by markush View Post
Hello kenny53067,

you should write some code by yourself and try it out. Learning programming doesn't work without experience. Just work in it.

Note that it is much easier for the advanced members here at LQ to help you with some code you've written.

Markus
I understand this 100%
I'm trying to understand what things do so that I will be able to write code.

example: I didn't know that ls Memo would not work, but ls Memo? would list all files with Memo in the name. Now I know.
 
  


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
Help writing a script... linux_evangelist Linux - Newbie 7 11-15-2010 07:19 AM
Need help writing script Rustylinux SUSE / openSUSE 1 12-05-2006 10:05 AM
Script writing help, somebody? cwwilson721 Slackware 6 05-19-2006 09:40 PM
Writing a Script Nyc0n Linux - General 2 05-27-2002 07:21 PM
writing a Script spanky5125 Linux - Security 5 01-08-2002 09:22 AM

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

All times are GMT -5. The time now is 02:03 PM.

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