LinuxQuestions.org
Help answer threads with 0 replies.
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-30-2008, 08:19 PM   #1
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

Rep: Reputation: 15
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
 
Old 07-30-2008, 08:20 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
echo $data > file

Is that what you want?

Create an empty file with:

touch file
or
> file
 
Old 07-30-2008, 08:54 PM   #3
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 270Reputation: 270Reputation: 270
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
 
Old 07-30-2008, 08:55 PM   #4
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
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
 
Old 07-30-2008, 08:58 PM   #5
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 270Reputation: 270Reputation: 270
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
 
Old 07-30-2008, 08:58 PM   #6
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 trickykid View Post
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
 
Old 07-30-2008, 09:02 PM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Remove the $ from in front of your variable assignment.
 
Old 07-30-2008, 09:21 PM   #8
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
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
 
Old 07-30-2008, 09:38 PM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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.
 
Old 07-30-2008, 09:59 PM   #10
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 270Reputation: 270Reputation: 270
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.

Last edited by trickykid; 07-30-2008 at 10:07 PM.
 
Old 07-30-2008, 10:13 PM   #11
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, 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

Last edited by FirstBorn; 07-30-2008 at 10:15 PM. Reason: Had to edit this line: FULL="/DIR/FILE"
 
Old 07-30-2008, 10:38 PM   #12
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
See Lecture 4 "Arguments", Notes: http://cis68b1.mikecappella.com/
 
Old 07-30-2008, 10:52 PM   #13
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 07-30-2008 at 11:15 PM.
 
Old 07-30-2008, 11:33 PM   #14
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
Arguments and Conditional Parameters...

Quote:
Originally Posted by Mr. C. View Post
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
 
Old 07-31-2008, 12:05 AM   #15
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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.
 
  


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 11:32 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
Open Source Consulting | Domain Registration