Create File Shell Syntax and pass data to the file
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Too bad they don't have a distro called Prozaklinux - I use Mandy ... (Mandriva 2008.1)
Posts: 64
Original Poster
Rep:
Thanks.
Quote:
Originally Posted by jschiwal
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.
Distribution: Too bad they don't have a distro called Prozaklinux - I use Mandy ... (Mandriva 2008.1)
Posts: 64
Original Poster
Rep:
Quote:
Originally Posted by Mr. C.
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.
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...
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.
Distribution: Too bad they don't have a distro called Prozaklinux - I use Mandy ... (Mandriva 2008.1)
Posts: 64
Original Poster
Rep:
I got it... That explains part of it... Thanks!
Quote:
Originally Posted by trickykid
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?
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.
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.
Distribution: Too bad they don't have a distro called Prozaklinux - I use Mandy ... (Mandriva 2008.1)
Posts: 64
Original Poster
Rep:
Got It, Thanks!
Quote:
Originally Posted by trickykid
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...
Distribution: Too bad they don't have a distro called Prozaklinux - I use Mandy ... (Mandriva 2008.1)
Posts: 64
Original Poster
Rep:
Quote:
Originally Posted by Mr. C.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.