LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Create File Shell Syntax and pass data to the file (https://www.linuxquestions.org/questions/linux-general-1/create-file-shell-syntax-and-pass-data-to-the-file-659371/)

FirstBorn 07-30-2008 08:19 PM

Create File Shell Syntax and pass data to the file
 
Hi,

Thanks for your help.

I've Googled and searched this forum and couldn't find exactly what I'm looking for.

Looking for the Shell syntax is to create a file,
not a directory, but a File...
(# /bin/bash )

I know that I can just do something like:
Code:

kwrite /home/firstborn/Desktop/firstborn.txt
and then save the file, but this has to be done in the background
when the script is executed.

I also need some data passed to the file, as well when being saved.
such as:
Code:

$data="ipsum dolar blah blah blah"
or something to that affect.

Thank You in advance for and I REALLY Appreciate Your help.

Thanks,
FirstBorn

Mr. C. 07-30-2008 08:20 PM

echo $data > file

Is that what you want?

Create an empty file with:

touch file
or
> file

trickykid 07-30-2008 08:54 PM

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


FirstBorn 07-30-2008 08:55 PM

Quote:

Originally Posted by Mr. C. (Post 3231511)
echo $data > file

Is that what you want?

Create an empty file with:

touch file
or
> file

Dear Mr. C,

Thanks for helping!

It's ALMOST what I'm looking for, with the exception of
one thing: My Data variable isn't right...

Here is what I have:
Code:

#!/bin/bash

$data="ipsum dolar blah blah blah"

echo $data> /home/firstborn/Desktop/testpass.txt

I'm getting the following error:
Quote:

/usr/bin/testwrite: line 3: =ipsum dolar blah blah blah: command not found
What am I doing wrong?
or, what is the correct syntax for setting data to a variable?
(Am I ASKing that correctly?)

Thanks,
FirstBorn

trickykid 07-30-2008 08:58 PM

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:

Example usage would be:

./script.sh filename-you-want-here

FirstBorn 07-30-2008 08:58 PM

Quote:

Originally Posted by trickykid (Post 3231546)
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.

Thanks,
FirstBorn

Mr. C. 07-30-2008 09:02 PM

Remove the $ from in front of your variable assignment.

FirstBorn 07-30-2008 09:21 PM

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.

Thanks,
FirstBorn

Mr. C. 07-30-2008 09:38 PM

Here's the basic idea:

Code:

#!/bin/bash

dir="$1"
file="$2"
fullpath="$1/$2"

echo $data > "$fullpath"

You can fill in the remainder and details.

trickykid 07-30-2008 09:59 PM

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.

Usage example:
./script.sh /path/to/directory 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.

FirstBorn 07-30-2008 10:13 PM

Thanks, Mr. C. and trickykid,

Question, I'm a bit confused about $1 and $2...
Do I Replace $1 and $2 with the specific variables?
ex:
Code:

DIR="/home/firstborn/Desktop"
FILE="firstborn.txt"
FULL="DIR/FILE"
DATA="/home/firstborn/datafile"

If NOT, then, would You be able to explain a little further?
(ex. how to set $1 and $2)

Thanks,
FirstBorn

Mr. C. 07-30-2008 10:38 PM

See Lecture 4 "Arguments", Notes: http://cis68b1.mikecappella.com/

jschiwal 07-30-2008 10:52 PM

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.

FirstBorn 07-30-2008 11:33 PM

Arguments and Conditional Parameters...
 
Quote:

Originally Posted by Mr. C. (Post 3231608)
See Lecture 4 "Arguments", Notes: http://cis68b1.mikecappella.com/

Hi Mr. C.,

Thanks for the URL...
That's something I've been looking for...

I can understand what they're talking about in regards to
arguments, however, I'm left blank when looking at this code:
Code:

DIR=$1
FILE=$2
FULL="$1/$2"
DATA="your data source"

...because:
There doesn't seem to be a group of command line arguments to
grab the first and second 'positional parameters' mentioned in
the code.

How would the code above get:
"/home/firstborn/Desktop/"
AND
"file.txt"
out of $1 and $2 IF there is NO variable SET to those parameters?

I'm still lost...

Am I completely missing this?

Thanks,
FirstBorn

Mr. C. 07-31-2008 12:05 AM

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...

They == me.


All times are GMT -5. The time now is 08:12 PM.