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 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" Thank You in advance for and I REALLY Appreciate Your help. Thanks, FirstBorn |
echo $data > file
Is that what you want? Create an empty file with: touch file or > file |
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 |
Quote:
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 Quote:
or, what is the correct syntax for setting data to a variable? (Am I ASKing that correctly?) Thanks, FirstBorn |
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 |
Quote:
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 |
Remove the $ from in front of your variable assignment.
|
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 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 |
Here's the basic idea:
Code:
#!/bin/bash |
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 |
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" (ex. how to set $1 and $2) Thanks, FirstBorn |
See Lecture 4 "Arguments", Notes: http://cis68b1.mikecappella.com/
|
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. |
Arguments and Conditional Parameters...
Quote:
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 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 |
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:
|
All times are GMT -5. The time now is 08:12 PM. |