ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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
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
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.