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.
Maybe look something like this as I'd make it so the name of the file is read when you execute the script on the command line to give you control over the filename:
Code:
#!/bin/bash
# lets cd into the present working directory to create the file
cd $PWD
# Create the file but first check to make sure it's doesn't already exist:
if [ -f "$1" ]; then
echo "$1 already exists in $PWD" # file exists
else
touch $1 # file didn't exist, let's create it.
fi
# 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"
# Add the data to the new file:
echo $DATA > $1
Don't put the $ sign to declare the variable. Look at my example. You should make your scripts somewhat intelligent by maybe first checking if the file exists so you don't overwrite any existing files that might have the same name, etc. My script also gives you control over what you want to call the filename when you run the script:
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 trickykid
Maybe look something like this as I'd make it so the name of the file is read when you execute the script on the command line to give you control over the filename:
Code:
#!/bin/bash
# lets cd into the present working directory to create the file
cd $PWD
# Create the file but first check to make sure it's doesn't already exist:
if [ -f "$1" ]; then
echo "$1 already exists in $PWD" # file exists
else
touch $1 # file didn't exist, let's create it.
fi
# 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"
# Add the data to the new file:
echo $DATA > $1
Hi trickykid!
Thanks for helping.
k, that is near perfect on checking for the file name.
Question, how would I set it to save (for now) on the Desktop?
I would change it as needed when ready to use.
Distribution: Too bad they don't have a distro called Prozaklinux - I use Mandy ... (Mandriva 2008.1)
Posts: 64
Original Poster
Rep:
Mr. C's works, got to understand trickykid's script
Mr. C., Thanks!
Yours works now.
---------------------
trickykid, Thanks.
I'm kind of confused.
I like the way that You've set this script up.
Here is what I would like to do if You're willing to help.
1. Set a variable to where the file is to be saved,
2. Set a variable as to what to name the file,
(hard coded for now, will change later on...)
As to Your current code:
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"
Yes, I DO have something in mind...
I WILL be grabbing data from a different source.
In fact, I am going to have to use that data to create 4 different files
and put things into a format from a few template files.
But, that comes later...
I want to LEARN this part FIRST.
I will explain My MAIN Goal(s) with this code (Why I posted this Q,)
later on.
One of My MAIN Goals is to LEARN the Code provided in this thread.
Yeah, I'd take the approach of taking command line arguments that you want. You could even take my first example and put the full path of the file instead of using $PWD to change the directory of where you run the script from, etc. Just rip out the $PWD piece or do what Mr. C suggested. Have first variable be the DIR and the second be the FILENAME.
And you don't even have to use my example of touching the file either, the script could look like this:
Code:
#!/bin/bash
DIR=$1
FILE=$2
FULL="$1/$2"
DATA="your data source"
# 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
You could even add the data source as an option like the DIR and FILE as well. Get creative and add a usage parameter if not all the options are met, etc.
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.
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.
Quote:
Originally Posted by FirstBorn
I can understand what they're talking about in regards to...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.