LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 02: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, 03:19 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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, 03: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, 03:47 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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, 03: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, 05:12 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Think about what you are saying, would the following work with your logic:
Code:
$ ls g
g8
 
Old 07-23-2011, 05: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, 05:25 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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, 05:28 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 05: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, 05:42 PM   #11
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
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, 05:48 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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, 05:54 PM   #13
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 495

Rep: Reputation: 212Reputation: 212Reputation: 212
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, 06:00 PM   #14
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
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, 06: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


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


All times are GMT -5. The time now is 08:12 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