Shell script explanation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script explanation
# 8  
Old 02-01-2018
From the 15 names, 10 names do not contain "a" (grep a fails) so they go to grep B

Last edited by rdrtx1; 02-01-2018 at 07:20 PM..
# 9  
Old 02-01-2018
Ah now I understand this part but was the part correct by me with 15 times? Because it is the first instruction ?
# 10  
Old 02-01-2018
Yes. All 15 names get evaluated by grep a first.
# 11  
Old 02-02-2018
Let us go back to your original code (with a space added between echo and $WORT and with the missing slash character added before dev/null):
Code:
i=0
while read WORT
do
 echo $WORT|grep a>/dev/null || echo $WORT|grep B>/dev/null || let i=$i+1
done
echo $i

You know what the first line does.

The read WORT reads the next line from standard input; removes the trailing <newline>; and, as long as the last character in the remainder of that line is not a backslash (\) character, returns an exit code 0 which causes the while to run the code between the following do and done. If there the last character of a line was a backslash character, read will also remove the backslash character; read the next line from standard input; and append its contents to WORT recursively until a line is found that does not end with a backslash character. This continues repeatedly until an I/O error is encountered or an EOF is reached while reading from standard input. Either of these conditions will cause read to exit with a non-zero exit status causing the loop to be terminated.

Moving on to the grep commands inside the loop... The command:
Code:
echo $WORT|grep a>/dev/null

uses echo to write one line (after removing trailing backslash characters and merging lines) to grep a > /dev/null
The grep utility will read that line and if it contains one or more lowercase latin letter a (a) characters, it will copy that line to its standard output (in this case redirected to /dev/null) and return success (i.e., a 0 exit status). Otherwise, nothing will be written to standard output and grep will return failure (i.e., a non-zero exit status).

In an OR-list (i.e.; a list of pipelines separated by the shell || operator, the 1st pipeline in the list is executed and its exit status is evaluated. If that pipeline succeeds, the OR-list is complete at that point; any remaining pipelines in the OR-list will be skipped; and the exit status of the OR-list will be success (i.e., exit code 0). If the first pipeline in the OR-list failed (i.e., returned a non-zero exit code), the next pipeline in the list is executed and its exit status is evaluated just like the 1st pipeline in the list. If no pipeline in the list succeeded, the exit status of the OR-list will be the exit status of the last pipeline executed. If any pipeline in the OR-list succeeded, the exit status of the OR-list will be success (i.e., 0).

An OR-list could also be written as a nested if statement. A nested if statement producing the same results as the OR-list in the loop in your code could also be written as:
Code:
	if echo $WORT|grep a>/dev/null
	then	continue
	else 	if echo $WORT|grep B>/dev/null
		then	continue
		else	let i=$i+1
		fi
	fi

As we can see from this (if it wasn't obvious when reading the OR-list n your code), this increments the value of the variable i for each line read from your script's standard input that contains neither a lowercase latin a nor an uppercase latin B.

Note that executing grep once or twice for each line in a text file containing lots of input lines is a slow and compute intensive operation. You haven't given us any sample input data and you haven't given us any indication of the sizes of the input files you will be processing, so we can only make wild comments about possible alternatives... For example, if your input files do not contain any continuation lines (i.e. lines ending with a backslash character immediately followed by <newline> character), your entire script could be replaced by a much simpler (and, for large files, much faster) single grep command:
Code:
grep -cv '[aB]'

If your input does contain continuation lines, your input files are relatively long, you want continuation lines to be merged into single lines before looking for the characters you're trying to match, you could still replace your script with an awk script that would join continuation lines (like read does when invoked without the -r option), look for the two patterns on joined lines and increment a count for lines that don't match either pattern, and print the count when it hits EOF on its input. (I'll leave writing this simple awk script as an exercise for the reader. With no description of the data that your script will be processing, there is no reason to waste the time writing a script like that if your input files will never contain continuation lines.)

I hope this helps you better understand what your script is doing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

What does this shell script do? Need in depth explanation please

Hi My friend wrote this particular script and won't tell me what it does, and when I run it I don't understand it. What does the entire script do with specifics please. Thanks Bob #!/bin/bash current=0 while ; do if ; then echo ${current} current=$((${current}+1)) fi done (1 Reply)
Discussion started by: shadowknight777
1 Replies

2. Solaris

Explanation of script

Hello Guys, can someone help explain the script below for me? I will really appreciate it. vi db_script #!/bin/sh echo .cron job run on.`date`> cronjob.txt df -h >> cronjob.txt echo welcome to home (2 Replies)
Discussion started by: cjashu
2 Replies

3. Shell Programming and Scripting

need an explanation on this script...

The following script will create a directory in a directory and will go on as many times as the number you will give in. I am trying to find out how it works ... can someone please help me with that? #!/bin/sh #create a variable and set it to 1 n=1 #start a loop as... (3 Replies)
Discussion started by: I-1
3 Replies

4. Shell Programming and Scripting

Script explanation

#script fileused=test.txt hostname=test.dis.com ftp $hostname <<-! > $fileused.err 2>&1 put file.txt /usr/text.txt bye ! kindly the above script the one marked as Bold and underlined as the above i am declaring the new variable as filename ,But when i used i had used as $fileused.err... (1 Reply)
Discussion started by: rajar_r
1 Replies

5. Shell Programming and Scripting

Shell Script Explanation

Hello, I have seen this script on this site. I understand most of it. However I am a bit stuck on the part in red. It appears to be expanding for file in *.zip do zipdir=${file%.*} mkdir $zipdir || echo "unable to create $zipdir" cp $file $zipdir || echo "unable to copy $file"... (3 Replies)
Discussion started by: jaysunn
3 Replies

6. Shell Programming and Scripting

explanation for a script

Guys, was wondering what the meaning of the below bit is ? awk -F ' ' '{print $1 " " $2 ;}' $TEMPFILE | (rm -f $TEMPFILE; sed 's/$/ '"$box"'/g' > $TEMPFILE) Can anyone explain this in detail? what is the significance of rm -f $TEMPFILE here? What all IO/buffering happens here ?How the... (0 Replies)
Discussion started by: hashin_p
0 Replies

7. Shell Programming and Scripting

Need explanation of script

Hi All, Can anybody explain what this script is doing? #!/bin/sh who | cut -d " " -f1 | sort -u > userlist1 while true ; do sleep 60 who | cut -d" " -f1 | sort -u >userlist2 for username in `cat userlist1` ; do if ! grep "^$username$" userlist2 > /dev/null ; then echo... (0 Replies)
Discussion started by: vishalpatel03
0 Replies

8. Shell Programming and Scripting

Explanation of running this script

I have a script that has defined a log file like this. The name of the script is verify.sh Inside the script there is some thing like this. LOG=/usr/verify TDATE=`date "+%m%d%y"$$` LOGFILE=$LOG.$TDATE. and inside the script it has been written as echo "This is to verify" | tee -a... (2 Replies)
Discussion started by: sendhilmani
2 Replies

9. Shell Programming and Scripting

Script explanation

I have the following script awk '$1 ~ /^*+/ { s += $NF; m++ } END { print NR, m, s } and I use it to get results from the following file A4792 4 COMP9021 5 K9 7 ABC 8 924 1 R2D2 3 (8 Replies)
Discussion started by: sickboy
8 Replies

10. Shell Programming and Scripting

any explanation for thsi shell script behaviour

hello whats the difference between excuting a shell script as a)sh myscript.sh b). ./myscript.sh i noticed that my shell script works fine when i run it as . ./myscript .sh but fails when i run it as sh myscript.sh could anybody explain why. the shell script is very simple ... (9 Replies)
Discussion started by: xiamin
9 Replies
Login or Register to Ask a Question