LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-31-2008, 07:02 AM   #16
FirstBorn
Member
 
Registered: Jul 2008
Location: Hell - aka - Florida
Distribution: Too bad they don't have a distro called Prozaklinux - I use Mandy ... (Mandriva 2008.1)
Posts: 64

Original Poster
Rep: Reputation: 15
Thanks.


Quote:
Originally Posted by jschiwal View Post
One technique for create files from a script is to use here documents inside the script. You can have several here documents in a script and have them redirected to a file. This is how many install scripts worked in Unix before package systems like rpm were written.
The here documents can contain variables which are expanded before the file is written.
Here documents can make it easy to produce a file from a template. For example, you can have an html header written first, then then append a text file, and finally add the last part. This will produce a simple html text file.

See the info bash manual for details on here documents.
Hi jschiwal,

Thanks for adding this.

I might eventually do this,but I'll STILL have to learn
what Mr. C. is showing First.
... And, I'll have to learn more about 'here documents.'

I actually was reading something about here documents the
other day.

Thanks,
FirstBorn
 
Old 07-31-2008, 07:12 AM   #17
FirstBorn
Member
 
Registered: Jul 2008
Location: Hell - aka - Florida
Distribution: Too bad they don't have a distro called Prozaklinux - I use Mandy ... (Mandriva 2008.1)
Posts: 64

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mr. C. View Post
The code I gave you was a shell script. You've omitted the #!/bin/bash at the top of the script, which is the clue that the script is meant to be executed via command line. As such, when you call it via command line, the shell automatically sets the positional parameters are $1, $2, ...

In C or perl, these are the ARGV parameters.

Look at the code trickykid provided.


They == me.
Hi Mr. C.

Quote:
Originally Posted by Mr. C. View Post
They == me.
That is so Cool!
I'm getting the 'source' from the 'source'


Quote:
You've omitted the #!/bin/bash at the top of the script, which is the clue that the script is meant to be executed via command line. As such, when you call it via command line, the shell automatically sets the positional parameters are $1, $2, ...
Which gets Me back to the Q:
"Where does the data come from for that script?"
(where I'm Not entering info into the command line
and running it from a script.)

Please forgive My confusion.

To My Understanding, the DATA Variable is where the data source is coming from, like, example.sh or
Code:
# Data
DATA="data here. be creative lots of ways to add data to a file from other sources rather than having static data in this script"
IF the argument was to grab positional parameters from the "DATA" above, this would be wrong because the location to the file might be;
/home/firstborn/Desktop
The script will be grabbing the DATA,
NOT the location.
Ex. The positional parameters will only allow /home/file.txt
IF it's using $1/$2
and it NEEDS the location variables.

There is an issue between the actual Data and the File Location,
is what I'm saying...

Which brings Us back to $1/$2...
Where would I find these / How can I set these variables?

Am I thinking on the right track here?

Thank You for Your help.

Thanks,
FirstBorn

Last edited by FirstBorn; 07-31-2008 at 08:09 AM. Reason: Had to add to data source...
 
Old 07-31-2008, 08:53 AM   #18
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by FirstBorn View Post
Hi Mr. C.


That is so Cool!
I'm getting the 'source' from the 'source'




Which gets Me back to the Q:
"Where does the data come from for that script?"
(where I'm Not entering info into the command line
and running it from a script.)

Please forgive My confusion.

To My Understanding, the DATA Variable is where the data source is coming from, like, example.sh or
Code:
# Data
DATA="data here. be creative lots of ways to add data to a file from other sources rather than having static data in this script"
IF the argument was to grab positional parameters from the "DATA" above, this would be wrong because the location to the file might be;
/home/firstborn/Desktop
The script will be grabbing the DATA,
NOT the location.
Ex. The positional parameters will only allow /home/file.txt
IF it's using $1/$2
and it NEEDS the location variables.

There is an issue between the actual Data and the File Location,
is what I'm saying...

Which brings Us back to $1/$2...
Where would I find these / How can I set these variables?

Am I thinking on the right track here?

Thank You for Your help.

Thanks,
FirstBorn
I provided this previously. When you save this script and you run such script from the command line, $1 and $2 are provided from the command line after the script name.

Say for example, you name this script blah.sh. chmod +x blah.sh so it's executable, you would then run the command like this:

Code:
./blah.sh /path/to/dir filename
In the above example, /path/to/dir will get passed as $1 and filename would get passed as $2.

Does that help explain where they come from?
 
Old 07-31-2008, 09:40 AM   #19
FirstBorn
Member
 
Registered: Jul 2008
Location: Hell - aka - Florida
Distribution: Too bad they don't have a distro called Prozaklinux - I use Mandy ... (Mandriva 2008.1)
Posts: 64

Original Poster
Rep: Reputation: 15
I got it... That explains part of it... Thanks!

Quote:
Originally Posted by trickykid View Post
I provided this previously. When you save this script and you run such script from the command line, $1 and $2 are provided from the command line after the script name.

Say for example, you name this script blah.sh. chmod +x blah.sh so it's executable, you would then run the command like this:

Code:
./blah.sh /path/to/dir filename
In the above example, /path/to/dir will get passed as $1 and filename would get passed as $2.

Does that help explain where they come from?
Hi trickykid,

k, Now I understand that part...
I didn't get it because I didn't remember seeing anything
about entering data into the command line (Konsole, or whatever...)

How would I set this script to work where the directory is
hard coded but STILL seeks to find if the file has been created
or not?

That is, Hard coded into the script, so that if I call the file:
ex.
Code:
# scriptname filename
it will seek the directory (hard coded into the file) to
find a duplicate name, and if not, go ahead and create the file,
pass the data to the file, then the file will be saved in
the specific location that is hard coded into the file.

I don't think that I can use:
1="/path/to/dir"
or
2="filename"
especially, according to Mr. C.'s "Arguments" lesson.
Am I correct?

Thanks,
FirstBorn
 
Old 07-31-2008, 10:09 AM   #20
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Well, if you hard code the DIR instead of using $1 that specifies this when running the script, simply change this:

Code:
#!/bin/bash

DIR="/home/firstborn/Desktop"
FILE="$1"
FULL="$DIR/$FILE"
DATA="/home/firstborn/datafile"

# Create the file but first check to make sure it's doesn't already exist:
if [ -f "$1/$2" ]; then
    echo "A file with the name $2 already exists in $1" # file exists
  else
    echo $DATA > $FULL
fi
To:

Code:
#!/bin/bash

DIR="/home/firstborn/Desktop"
FILE="$1"
FULL="$DIR/$FILE"
DATA="/home/firstborn/datafile"

# Create the file but first check to make sure it's doesn't already exist:
if [ -f "$DIR/$1" ]; then
    echo "A file with the name $1 already exists in $DIR" # file exists
  else
    cat $DATA > $FULL
fi
You'll notice the line echo $DATA > $FULL has changed to cat $DATA > $FULL since you can't echo a filename to see the contents, it will just echo what you type. You'll also noticed for the FULL variable, you need to put $ signs in front of DIR and FILE so they are read properly.
 
Old 07-31-2008, 11:38 AM   #21
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
FirstBorn, its clear you are interested in learning shell scripting, and seem to enjoy it.

It is difficult to build a solid foundation of understanding when you start building in the middle. I'd like to recommend that you step back, and solidify some of the basics that you are missing here. The questions you ask are good ones, but seem to indicate that your missing some fundamentals. Not resolving those now will lead to wasted time, misunderstandings, and some amount of "voodoo" beliefs about how the systems works.

There is elegance to the system, but you it needs to be presented and learned from a certain perspective.

Let me suggest that you revisit some of the first lessons. If you like the material I referenced, start back at Week 1. This course, as all my courses, were designed with a certain sequence of building blocks. While the online course was designed to work with a certain text, I think there is plenty of value from the materials standalone, especially the homework exercises. The answers are also available if you really get stuck, but the learning comes only with repeated attempts and plenty of failure. Its the exploration and practice that matters, not the end result.
 
Old 07-31-2008, 09:40 PM   #22
FirstBorn
Member
 
Registered: Jul 2008
Location: Hell - aka - Florida
Distribution: Too bad they don't have a distro called Prozaklinux - I use Mandy ... (Mandriva 2008.1)
Posts: 64

Original Poster
Rep: Reputation: 15
Got It, Thanks!

Quote:
Originally Posted by trickykid View Post
Well, if you hard code the DIR instead of using $1 that specifies this when running the script, simply change this:

Code:
#!/bin/bash

DIR="/home/firstborn/Desktop"
FILE="$1"
FULL="$DIR/$FILE"
DATA="/home/firstborn/datafile"

# Create the file but first check to make sure it's doesn't already exist:
if [ -f "$1/$2" ]; then
    echo "A file with the name $2 already exists in $1" # file exists
  else
    echo $DATA > $FULL
fi
To:

Code:
#!/bin/bash

DIR="/home/firstborn/Desktop"
FILE="$1"
FULL="$DIR/$FILE"
DATA="/home/firstborn/datafile"

# Create the file but first check to make sure it's doesn't already exist:
if [ -f "$DIR/$1" ]; then
    echo "A file with the name $1 already exists in $DIR" # file exists
  else
    cat $DATA > $FULL
fi
You'll notice the line echo $DATA > $FULL has changed to cat $DATA > $FULL since you can't echo a filename to see the contents, it will just echo what you type. You'll also noticed for the FULL variable, you need to put $ signs in front of DIR and FILE so they are read properly.
Hey trickykid,

Thanks!

I got it...
So, basically, the $1 will be what was typed in at the command prompt?
(in the Konsole.)
Now I get it...

Thanks,
FirstBorn
 
Old 07-31-2008, 09:48 PM   #23
FirstBorn
Member
 
Registered: Jul 2008
Location: Hell - aka - Florida
Distribution: Too bad they don't have a distro called Prozaklinux - I use Mandy ... (Mandriva 2008.1)
Posts: 64

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mr. C. View Post
FirstBorn, its clear you are interested in learning shell scripting, and seem to enjoy it.
Thanks, Mr. C.,

Yes, Very much so...

It's like when I was learning DOS about 12 years ago.
(and VB 5 and 6 in the late 90's.)

It can be VERY Addictive.


Quote:
It is difficult to build a solid foundation of understanding when you start building in the middle. I'd like to recommend that you step back, and solidify some of the basics that you are missing here. The questions you ask are good ones, but seem to indicate that your missing some fundamentals. Not resolving those now will lead to wasted time, misunderstandings, and some amount of "voodoo" beliefs about how the systems works.

There is elegance to the system, but you it needs to be presented and learned from a certain perspective.

Let me suggest that you revisit some of the first lessons. If you like the material I referenced, start back at Week 1. This course, as all my courses, were designed with a certain sequence of building blocks. While the online course was designed to work with a certain text, I think there is plenty of value from the materials standalone, especially the homework exercises. The answers are also available if you really get stuck, but the learning comes only with repeated attempts and plenty of failure. Its the exploration and practice that matters, not the end result.
Thank You VERY Much for Your help and suggestions.

I have downloaded every lesson on Your cis course found here:
http://cis68b1.mikecappella.com/
... and will be going through it with a fine tooth comb...

Also, Thank You for having that available to learn from.



Thanks,
FirstBorn
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
use script to create a data file of mulitple users RajRed Linux - Newbie 4 11-27-2007 06:05 PM
How to read data from file to use in shell script? ozymandias Linux - Newbie 7 10-27-2006 01:19 PM
how to send shell variable data to a text file ginda Programming 7 06-23-2006 05:49 AM
gave wrong syntax for tar as tar -cvzf file file.tgz how to recover the file gautham Linux - General 4 04-13-2005 03:15 AM
Linux shell command for makefile.in to create a text file and write to it alix123 Programming 8 01-07-2005 08:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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