LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-12-2009, 10:27 AM   #1
boyd98
Member
 
Registered: Oct 2003
Posts: 156

Rep: Reputation: 15
Can someone help me outline a ksh script:


I want to be able to grep file X for Y and if its not exist i want it to write it Y out to another file; and then echo Y out

Where X is a list.

-----------------
cat file1
1
2
3
4
5
--------------

so if i grep file1 for 6 it will put the number 6 in another file and echo out 6 does not exist

Last edited by boyd98; 02-12-2009 at 10:46 AM.
 
Old 02-12-2009, 10:51 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
It looks like an homework question, doesn't it? Take a look at the if/then construct using a command in place of a logical expression. The Advanced Bash Scripting Guide, Chapter 7.1 explains it all. No difference between bash and ksh for this issue!
 
Old 02-12-2009, 02:14 PM   #3
boyd98
Member
 
Registered: Oct 2003
Posts: 156

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
It looks like an homework question, doesn't it? Take a look at the if/then construct using a command in place of a logical expression. The Advanced Bash Scripting Guide, Chapter 7.1 explains it all. No difference between bash and ksh for this issue!
lol - yea not homework -

I'm actually in the middle of an install and i dont script frequently, so my knowledge doesnt stay fresh.

I'm good, i spent some time on it after i wrote the question


i had a list of packages on hpux, 251, that i needed to confirm were installed.



Thx again
 
Old 02-12-2009, 03:18 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Ok. You can try a test like this:
Code:
pattern=6
if ! grep -q $pattern file1
then
  echo $pattern > another_file
  echo "Pattern $pattern not found!"
fi
The -q option of grep stats for quiet mode, that prevents the output to be displayed on the terminal. The exclamation mark is the negation of the test: if the pattern is not found, the echo statements are executed. Take in mind that each command in linux spits out an exit status. Usually the exit status for success is 0, which is interpreted by the shell as true. An exit code different than 0 (usually 1) is interpreted as false.

You can demonstrate it running the following:
Code:
$ cat file1
0
1
2
3
4
5
$ grep 6 file1
$ echo $?
1
The shell variable $? stores the exit status of the previous command.
 
Old 02-12-2009, 08:04 PM   #5
servat78
Member
 
Registered: Jan 2009
Posts: 100

Rep: Reputation: 17
If you are more familiar with PHP then with KSH, then make it a PHP script and launch it from the command-line, like '$ php myscript.php'.
I have found it more easier for my own work to switch to a scripting language that I use frequently instead of shell scripting that are a bit cumbersome anyway.

Debian

Last edited by servat78; 02-19-2009 at 12:22 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Ksh Script Help sharathkv25 Programming 10 02-02-2007 11:28 AM
help w/ksh script ShaqDiesel Programming 2 01-12-2007 03:04 PM
gpg from ksh script Risc91 AIX 2 05-11-2005 11:45 AM
Can somebody help me with a ksh script? twentymil AIX 7 01-21-2004 10:55 AM
KSH script AquamaN Programming 2 12-08-2003 12:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:59 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