LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2007, 05:35 PM   #1
Coldfan20
LQ Newbie
 
Registered: Aug 2007
Posts: 8

Rep: Reputation: 0
shell scripts


Im trying to write a shell script and write the data to another file and i can not find the syntax any where does any one know the syntax to help me out thanks
 
Old 08-08-2007, 05:47 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
What books are you using? Two to start with are:
Bash Guide for Beginners, Machtelt Garrels---free at tldp.org
Classic Shell scripting from O'Reilly (might be available in their new free online service.)

Here is one example of reading and writing files using redirection.

This reads data from file1, replaces all occurrences of "yes" with "no", and writes the data to file2:

sed 's/yes/no/g' <file1 >file2
This does exactly the same thing:
cat file1 | sed 's/yes/no/g' >file2
 
Old 08-08-2007, 05:54 PM   #3
Coldfan20
LQ Newbie
 
Registered: Aug 2007
Posts: 8

Original Poster
Rep: Reputation: 0
i want it to write to the file right after all the information is input, with the code that you gave me my script should look like this i beleive am i right?

#!/bin/bash
echo “enter employee name”
read employee name
echo “age”
read age
echo “dept num”
read dept num
cat file1 | sed 's/yes/no/g' >employee.db



Quote:
Originally Posted by pixellany
What books are you using? Two to start with are:
Bash Guide for Beginners, Machtelt Garrels---free at tldp.org
Classic Shell scripting from O'Reilly (might be available in their new free online service.)

Here is one example of reading and writing files using redirection.

This reads data from file1, replaces all occurrences of "yes" with "no", and writes the data to file2:

sed 's/yes/no/g' <file1 >file2
This does exactly the same thing:
cat file1 | sed 's/yes/no/g' >file2

Last edited by Coldfan20; 08-08-2007 at 06:10 PM.
 
Old 08-08-2007, 06:40 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You can't have spaces in variable names, use underscores.
Read this page: http://tldp.org/LDP/abs/html/io-redirection.html
Also: http://tldp.org/LDP/abs/html/x16239.html#USINGEXECREF
 
Old 08-08-2007, 08:37 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by Coldfan20
i want it to write to the file right after all the information is input, with the code that you gave me my script should look like this i beleive am i right?

#!/bin/bash
echo “enter employee name”
read employee name
echo “age”
read age
echo “dept num”
read dept num
cat file1 | sed 's/yes/no/g' >employee.db
I only gave you an example.....not a literal command. As you have it stated above, the last line will do nothing. (eg "file1" does not exist)

Let's assume you have successfully created a variable "dept_num". To write that variable to your employee.db file, you would do:
echo $dept_num >employee.db
Now to add the contents of age, you would do:
echo $age >>employee.db
Note that >> = append

The best way to learn this stuff is the try one command at time in the interactive mode. Once you know how a command works, then you can put it in your script.
 
Old 08-08-2007, 10:50 PM   #6
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by pixellany
I only gave you an example.....not a literal command. As you have it stated above, the last line will do nothing. (eg "file1" does not exist)

It doesn't do nothing; it will give you an error message.

Quote:
Let's assume you have successfully created a variable "dept_num". To write that variable to your employee.db file, you would do:
echo $dept_num >employee.db
Now to add the contents of age, you would do:
echo $age >>employee.db
Note that >> = append

Better would be to group the commands into a single compound command (using braces) and a single redirect:

Code:
{
  echo "$dept_num"
  echo "$age"
} > employee.db
And note the quoting of the variables. This prevents filename expansion if there are any wildcard characters, and preserves spacing.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need shell scripts ARsenthil Linux - General 3 05-20-2007 12:55 PM
Shell Scripts baruah Linux - Certification 1 02-02-2006 01:19 PM
shell scripts newbie_m Linux - Newbie 3 01-18-2005 08:12 PM
shell scripts rocketgo Linux - General 10 12-01-2003 05:20 AM
shell scripts gui10 Programming 10 10-28-2001 02:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:03 PM.

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