LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Can someone help me outline a ksh script: (https://www.linuxquestions.org/questions/linux-software-2/can-someone-help-me-outline-a-ksh-script-704177/)

boyd98 02-12-2009 09:27 AM

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

colucix 02-12-2009 09:51 AM

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!

boyd98 02-12-2009 01:14 PM

Quote:

Originally Posted by colucix (Post 3441104)
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

colucix 02-12-2009 02:18 PM

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.

servat78 02-12-2009 07:04 PM

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


All times are GMT -5. The time now is 01:56 AM.