Why the results of these two code fragments are not the same?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why the results of these two code fragments are not the same?
# 8  
Old 02-07-2019
I generally avoid eval, but have you considered making your shell script more like your C++ code?
Code:
#!/bin/sh
#set -xv
i=1
while [ $i -le $# ]
do	j=$((i + 1))
	while [ $j -le $# ]
	do	if eval [ \"\${$i}\" = \"\${$j}\" ]
		then	printf '\n%s (%d,%d)\n\nExiting...\n\n' \
			    'Error: Two or more arguments are the same:' \
			    $i $j >&2
			exit 1
		fi
		j=$((j + 1))
	done
	i=$((i + 1))
done

The while loops in the above script could be simplified to for loops that are accepted by many shells (including recent versions of bash and ksh) but they are extensions to the standards and, therefore, not available in several shells. The above code just uses standard utility interfaces and shell language constructs. For shells that accept these extensions, the below code looks even more like your C++ code.
Code:
#!/bin/sh
set -xv
for ((i = 1; i <= $#; i++))
do	for ((j = i + 1; j <= $#; j++))
	do	if eval [ \"\${$i}\" = \"\${$j}\" ]
		then	printf '\n%s (%d,%d)\n\nExiting...\n\n' \
			    "Error: Two or more arguments are the same:" \
			    $i $j >&2
			exit 1
		fi
	done
done

Note that both of these script write their error messages to the standard error output instead of to standard output and both of these script print the argument numbers of the first found pair of identical argument in their diagnostics.
# 9  
Old 02-07-2019
Quote:
Originally Posted by RudiC
Maybe the entire code could be condensed down to (tested with bash --posix)

Code:
TMPARR=( $@ )
for ((i=0; i<$#; i++))
  do    for ((j=i+1; j<$#; j++))
          do    [ ${TMPARR[i]} = ${TMPARR[j]} ] && { echo same arguments; exit 1; }
          done
  done

Thank you for your reply.

The code is not POSIX shell compliant. POSIX shell has not arrays, and not this kind of for loop.

I am in the half of my introductory POSIX shell book, so there are a lot of things I do not know.

But there is this. In POSIX shell all data are strings, and strings are a kind of array.

Here is an example:

Code:
#!/bin/sh

text="This is some text"

for x in $text
do
  # Do stuff with x.
done

exit 0

--- Post updated at 02:27 PM ---

Thank you for your reply.

Quote:
Originally Posted by Don Cragun
I generally avoid eval, but have you considered making your shell script more like your C++ code?
Code:
#!/bin/sh
#set -xv

I do not know anything about set yet.


Quote:
Code:
i=1
while [ $i -le $# ]
do	j=$((i + 1))
	while [ $j -le $# ]
	do	if eval [ \"\${$i}\" = \"\${$j}\" ]

I do not know anything about eval yet.


Quote:
Code:
		then	printf '\n%s (%d,%d)\n\nExiting...\n\n' \
			    'Error: Two or more arguments are the same:' \
			    $i $j >&2
			exit 1
		fi
		j=$((j + 1))
	done
	i=$((i + 1))
done

The while loops in the above script could be simplified to for loops that are accepted by many shells (including recent versions of bash and ksh) but they are extensions to the standards and, therefore, not available in several shells. The above code just uses standard utility interfaces and shell language constructs. For shells that accept these extensions, the below code looks even more like your C++ code.
Code:
#!/bin/sh
set -xv
for ((i = 1; i <= $#; i++))
do	for ((j = i + 1; j <= $#; j++))
	do	if eval [ \"\${$i}\" = \"\${$j}\" ]
		then	printf '\n%s (%d,%d)\n\nExiting...\n\n' \
			    "Error: Two or more arguments are the same:" \
			    $i $j >&2
			exit 1
		fi
	done
done

Note that both of these script write their error messages to the standard error output instead of to standard output and both of these script print the argument numbers of the first found pair of identical argument in their diagnostics.

OK, thank you for your reply. There are things that I do not know yet, and I will keep them in mind.

For now, I think the code I posted works OK.

Last edited by johnprogrammer; 02-07-2019 at 08:47 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I want to add a variable for the results from the formula of one variable and results of another var

Good morning all, This is the file name in question OD_Orders_2019-02-19.csv I am trying to create a bash script to read into files with yesterdays date on the file name while retaining the rest of the files name. I would like for $y to equal, the name of the file with a formula output with... (2 Replies)
Discussion started by: Ibrahim A
2 Replies

2. Shell Programming and Scripting

Extract fragments from file

I have a .xml file that looks something like this : <measInfo> ......... string1 ......... </measInfo> <measInfo> ...... string2 ........ </measInfo> I want to extract only the 'chunk of file' from '<measInfo>' to '</measInfo>' containing string1 (or a certain string that I... (13 Replies)
Discussion started by: black_fender
13 Replies

3. Shell Programming and Scripting

Why use different FS, the results is different?

# echo '1 2 3 ' | awk -F' ' '{print NF}' 3 # echo '1:2:3:' | awk -F':' '{print NF}' 4 (1 Reply)
Discussion started by: lazycat79
1 Replies

4. Shell Programming and Scripting

Can ctag and cscope support recording search results and displaying the history results ?

Hello , When using vim, can ctag and cscope support recording search results and displaying the history results ? Once I jump to one tag, I can use :tnext to jump to next tag, but how can I display the preview search result? (0 Replies)
Discussion started by: 915086731
0 Replies

5. IP Networking

Solaris 11 Express NAT/Router IP Fragments

Upon replacing my linux router/server with a Solaris one I've noticed very poor network performance. The server itself has no issues connecting to the net, but clients using the server as a router are getting a lot of IP fragments as indicated from some packet sniffing I conducted. Here was my... (3 Replies)
Discussion started by: vectox
3 Replies

6. Solaris

ipfilter blocking ip fragments

For some reason ipfilter is blocking inbound fragmented ip packets (the packets are larger than the interface's MTU) that are encapsulating UDP segments. The connection works, so I know ipfilter is letting some traffic through, it is just a lot slower than it should be. Rules that allow the... (3 Replies)
Discussion started by: ilikecows
3 Replies

7. Post Here to Contact Site Administrators and Moderators

results survey

Hi guys! I was wondering what the outcome was of your survey of a few months ago? One of the questions was if people were willing to pay for additional services like an own account, like username@unix.com with mail box, etc. Sorry if I missed the results if you had already posted them. Ivo (1 Reply)
Discussion started by: Ivo
1 Replies

8. UNIX for Advanced & Expert Users

fragments in Solaris 8

When discussing inodes and data blocks, I know Solaris creates these data blocks with a total size of 8192b, divided into eight 1024b "fragments." It stores data in "contiguous" fragments and solaris doesn't allow a file to use portions of two different fragments. If the file size permits, then the... (4 Replies)
Discussion started by: manderson19
4 Replies
Login or Register to Ask a Question