SH Script for Logical OR function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SH Script for Logical OR function
# 1  
Old 03-16-2012
SH Script for Logical OR function

Using good ole fashion bourne shell scripting I have to take 2 files and compare them 1:1 in a Logical OR "gate" or function and then output the info into a 3rd file. I have never tried anything like this before Smilie so any hand holding will be much "Thanked"

OR Gate being:
A, B, Result
0, 0, 0
0, 1, 1
1, 0, 1
1, 1, 1


file1
Code:
0010010101
0000000100
0000000000
0001010111
0000000000
0000000001
1000000000
0001010100
0000101010
0000000000

file2
Code:
0000100101
0001000010
0000000000
0001010111
0000000000
0001100001
1000010100
0001000100
0000101010
0001110000

file3 should be the following
Code:
0010110101
0001000110
0000000000
0001010111
0000000000
0001100001
1000010100
0001010100
0000101010
0000110000

# 2  
Old 03-16-2012
That's going to be a royal pain in plain bourne. That doesn't even have substring operators to retrieve those bits with, let alone proper math.

To read from the files:

Code:
# open files
exec 5<"file1"
exec 6<"file2"

while read LINE1 <&5 && read LINE2 <&6
do
        # Lines from file1 in LINE1
        # Lines from file2 in LINE2
done

# close files
exec 5<&-
exec 6<&-


To retrieve individual bits:

Code:
$ echo "01111" | cut -c 1
0

$ echo "10111" | cut -c 2
0

To do logic:

Code:
$ expr 0 "|" 1
1

$ expr 0 "|" 0
0

$

# 3  
Old 03-16-2012
One possible gash method, but making use of external commands (expr, paste). i.e. Not pure Bourne Shell.
Intended to illustrate the need to process each character side by side.
Pretty slow but it appears to work. If you are on an old Sun box, the syntax of "echo" with no terminator may be different "echo -n". The method for building up each output line can be much improved but this is an attempt at "open logic" regardless of efficiency.

Code:
#!/bin/sh
(
paste file1 file2 | while read line1 line2
do
        counter=1
        while [ ${counter} -le 10 ]
        do
                char1=`expr substr "${line1}" ${counter} 1` 
                char2=`expr substr "${line2}" ${counter} 1`
                # OR comparison
                if [ "${char1}" = "1" -o "${char2}" = "1" ]
                then
                        echo "1\c"      # Character with no line terminator
                else
                        echo "0\c"      # Character with no line terminator
                fi
                counter=`expr ${counter} + 1`
        done
        echo ""         # Terminate line
done
#
) | tee file3
echo ""
echo "Differences"
diff file3 file3.ref

./scriptname

0010110101
0001000110
0000000000
0001010111
0000000000
0001100001
1000010100
0001010100
0000101010
0001110000

Differences
10c10
< 0001110000
---
> 0000110000

For testing I copied the model output as file3.ref and compared. The last line appears to be in error.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

2. Shell Programming and Scripting

Function Script

First time doing a function script and I am getting an error. Anyone knows the problem? #!/bin/bash hello() { echo "Executing function hello" } echo "Script has started now" hello echo "Script will end" (3 Replies)
Discussion started by: rpiboy
3 Replies

3. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

4. Shell Programming and Scripting

What is the function of the following lines at the top of a shell script file: Directory and Script?

The file starts like this: Directory: <path to the script> Script: <script fife name> #!bin/ksh ##Comments <actual script> What is the use of the first two lines in the script? What if I save the file without them? What will be the effect? They are not comments. Im very new to this,... (4 Replies)
Discussion started by: remytom
4 Replies

5. Shell Programming and Scripting

Shell Script function to use script name for log file output

Hi Team - I"m very new to Shell Scripting so I have a rather novice question. My forte is Windows Batch Scripting so I was just wondering what the Shell Script equivalent is to the DOS command %~n? %~n is a DOS variable that dispayed the script name. For instance (in DOS): REM... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

6. UNIX for Dummies Questions & Answers

Logical OR in shell script

I have code as follows to perform some validations on C++ and Javascript files: if || || ; then However, when I want to add other extensions as well, say "py" or "sql", then the repeated OR starts to look contrived. I know I can use the -o operator to abbreviate the code a little bit, but... (14 Replies)
Discussion started by: figaro
14 Replies

7. Shell Programming and Scripting

How to implement the logical express --- A(B+C) within single "if" statment in shell script?

On Linux OS, bash environment, I want implement the following code: if && ( || ) A,B,C represents some compare conditions. How to realize it ? Thanks! (1 Reply)
Discussion started by: qcmao
1 Replies

8. Shell Programming and Scripting

Call shell script function from awk script

hi everyone i am trying to do this bash> cat abc.sh deepak() { echo Deepak } deepak bash>./abc.sh Deepak so it is giving me write simply i created a func and it worked now i modified it like this way bash> cat abc.sh (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

9. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

10. Shell Programming and Scripting

How to do logical AND and logical OR with grep

Hi can someone please help me on this. I need to perform this code: Grep any lines that meets the following criteria (A AND B) OR (A AND C) I tried this code, but it didn't work Grep-I "A &&B" | "A&&C" *.* $ thanks in advance (12 Replies)
Discussion started by: Needhelp2
12 Replies
Login or Register to Ask a Question