A few questions from a newbie(shell script)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A few questions from a newbie(shell script)
# 1  
Old 07-19-2007
A few questions from a newbie(shell script)

Q1>How do i read and write to file in shell script. Here is what i want let's assume the filename as "file1"

Read file1

Check the content of file1 which can be either "0" or "1"

if(content == 0)
{
execute a command
}

flush file1(remove all contents in it)

write "1" in to file1

execute a few commands

flush file1(remove all contents in it)

write "0" in to the file

Q2>How do i store the result of a shell script command in a variable.For example

"dropdb something" - When i execute this command on bash it gives a message
"database dropped" how do i capture this in shell and store it in a variable and compare it

Thanks for helping me!
# 2  
Old 07-19-2007
Answer for question number 2,

you can save the variable by usning "tee" command or redirect options..

1. command | tee

2. 2>&1 ( redirect stdout and stderr )
# 3  
Old 07-19-2007
Quote:
Originally Posted by srikanthus2002
Answer for question number 2,

you can save the variable by usning "tee" command or redirect options..

1. command | tee

2. 2>&1 ( redirect stdout and stderr )
So in my case the command is "dropdb" so will it be

"dropdb | tee"

But how do i store the outcome of the command in a var.

& i did not get the second option 2>&1 ( redirect stdout and stderr ) can u explain it to me with an example please
# 4  
Old 07-19-2007
try this

1. "dropdb | tee file_name"


2. dropdb > /home/sir/file_name 2>&1

#Here stand error and stand out get sent to the o/p file i.e. file_name

in both the cases message will be stored in file_name.

then you can processes it forther..

Last edited by srikanthus2002; 07-19-2007 at 08:53 AM..
# 5  
Old 07-19-2007
Is there any way i can transfer the output of a command to a variable.?

Last edited by perk_bud; 07-19-2007 at 09:34 AM..
# 6  
Old 07-21-2007
cat(print) the file and assign it to variable

Variable=`cat file_name`
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script newbie- how to generate service log from shell script

Hi, I am totally a newbie to any programming languages and I just started an entry level job in an IT company. One of my recent tasks is to create a script that is able to show the log file of linux service (i.e. ntpd service) lets say, if I run my script ./test.sh, the output should be... (3 Replies)
Discussion started by: xiaogeji
3 Replies

2. AIX

Newbie Questions for AIX !!!

Hi Guys, I am new in this forum and new with AIX however not new with Power System. I have worked with iSeries for many years. Now supporting AIX on Power. Here are some basic questions I have. 1. I am using Putty to connect from my PC to the AIX boxes. Is there any other (better) program to... (6 Replies)
Discussion started by: 300zxmuro
6 Replies

3. Shell Programming and Scripting

Perl newbie questions!

Hi, So I started to learn perl a few days ago, and I have some problems... One of my problems... #!C:\Perl64\bin\perl.exe -w use LWP::Simple; print "Content-Type: Text/Plain\n\n"; sub pagelinks { return @all = get($_) =~ /href\s*=\s*"?(+)/gis; } @a =... (5 Replies)
Discussion started by: byte1918
5 Replies

4. Solaris

Solaris Newbie questions...

Hello everyone, I am brand spanking new to both Solaris and Unix. I thought I would give it a go after buying a SB2500 off ebay for a few hundred dollars. I am having some issues that I am not sure how to correct, and I am wondering if I can get a few pointers? The first one is that my system... (2 Replies)
Discussion started by: GeekMasterFlash
2 Replies

5. UNIX for Dummies Questions & Answers

LISP newbie's questions

Hello, I want to learn LISP, and I have a GNU/Linux OS. I first sought a LISP compiler/interpreter and was told that GNU Emacs has a LISP mode. But I couldn't get into LISP mode, nor I don't know how to use it when I get into LISP mode. How can I run LISP code under GNU Emacs? And if... (1 Reply)
Discussion started by: rayne
1 Replies

6. Linux

Questions of a newbie

I have been an apple customer for years now, and am not satisfied with the direction that they are going. So I just ordered my first PC notebook the other day. I have no desire to use windows, however with microsoft's hold on the market, I feel that I may have a hard time doing this. I want to... (2 Replies)
Discussion started by: Brycemb16
2 Replies

7. Shell Programming and Scripting

Newbie Questions

I am relatively new to both KSH and Unix scripting, and I would like some help getting my script up and running. I would like to have the script attempt various commands (tar, copy, gzip etc) and then write the results (error msg or success msg) to a temp file. I would then like an email sent to... (2 Replies)
Discussion started by: mharley
2 Replies

8. UNIX for Dummies Questions & Answers

buncha questions from a newbie

Even though I have been logging in to a UNIX shell at school to complete school projects and write programs, but I had never really worked in UNIX environment. But a couple of weeks back I got hooked on to Solaris 9OE, read a book, a tutorial, a document provided on the Sun Microsystems website,... (1 Reply)
Discussion started by: init-5
1 Replies

9. UNIX for Dummies Questions & Answers

A few newbie questions

Hi :) I just wanted to ask a few basic questions really. I'm telnetting to a remote host and I've finally found out that I'm using a csh shell. My questions are: 1. Is the somename@something, the user group logged in? 2. How do I change user? I'm really lost so I hope someone can help... (7 Replies)
Discussion started by: hellz
7 Replies
Login or Register to Ask a Question