Help with Shell script code | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Help with Shell script code

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-12-2013
Geekie Geekie is offline
Registered User
 
Join Date: May 2013
Last Activity: 12 May 2013, 3:44 PM EDT
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Help with Shell script code

Hello all,

I am in a middle of an assignment and i would appreciate any help.
How can i write a bash shell script code that checks if all elements in an array are the same numbers. I mean -->array = ( 0,0,0,0,0 )

( e.g., if [ #all array elements equal zero ]
then return "OK'
fi )

Thank you in advance,
Sponsored Links
    #2  
Old 05-12-2013
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 1 August 2014, 6:54 AM EDT
Posts: 3,707
Thanks: 7
Thanked 1,141 Times in 1,112 Posts
Try:
Code:
#!/bin/bash
array=( 0 0 0 0 0 )
flag=0
for i in "${array[@]}"; do
  if [ $i -ne 0 ]; then
    flag=1
  fi
done
if [ $flag = "1" ]; then
  echo "not OK"
else
  echo "OK"
fi

Sponsored Links
    #3  
Old 05-12-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 1 August 2014, 12:35 PM EDT
Location: India
Posts: 1,820
Thanks: 24
Thanked 522 Times in 501 Posts

Code:
#! /bin/bash

arr=( 2 2 2 2 2 )

e=${arr[0]}
for x in ${arr[@]}
do
    if [ $x -eq $e ]
    then
        flag=1
        continue
    else
        flag=0
        break
    fi
done
    
if [ $flag -eq 1 ]
then
    echo "OK"
else
    echo "Not OK"
fi

    #4  
Old 05-12-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 31 July 2014, 10:48 PM EDT
Location: Amsterdam
Posts: 9,281
Thanks: 264
Thanked 2,303 Times in 2,066 Posts
How about:

Code:
left=${A[@]#"${A[0]}"}
if [ -z "$left" ]; then
  echo Ok
fi


Last edited by Scrutinizer; 05-13-2013 at 12:23 PM.. Reason: Added double quotes around A[0]
The Following 3 Users Say Thank You to Scrutinizer For This Useful Post:
balajesuri (05-12-2013), Scott (05-12-2013), Yoda (05-12-2013)
Sponsored Links
    #5  
Old 05-12-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 1 August 2014, 12:35 PM EDT
Location: India
Posts: 1,820
Thanks: 24
Thanked 522 Times in 501 Posts
Quote:
Originally Posted by Scrutinizer View Post
Code:
left=${A[@]#${A[0]}}

Excellent manipulation of parameter expansion Good one, Scrutinizer.
Sponsored Links
    #6  
Old 05-12-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 25 July 2014, 9:30 AM EDT
Posts: 1,038
Thanks: 62
Thanked 216 Times in 204 Posts
Can you explain the string manipulation here:
left=${A[@]#${A[0]}}

A[@] This list the complete array.
Sponsored Links
    #7  
Old 05-12-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 31 July 2014, 10:48 PM EDT
Location: Amsterdam
Posts: 9,281
Thanks: 264
Thanked 2,303 Times in 2,066 Posts
Hi Jotne, it takes the first element of the array and deletes the first portion of each element if that portion matches that first element. If all elements in array A are equal, then this will result in an array of empty strings. This result is then assigned to a variable, after which the variable should contain all non-empty array elements separated by spaces.

So if all array elements are identical, then the result should be an array of empty strings and then the variable should contain an empty string.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script shell in java code chercheur857 Programming 9 05-12-2012 08:39 AM
Code java in script shell chercheur857 Shell Programming and Scripting 0 04-27-2012 05:37 PM
Script Shell in java code chercheur857 Programming 2 04-07-2012 06:17 PM
Insert C code in shell script badbunny9316 Shell Programming and Scripting 1 05-14-2009 02:55 AM
Pl explain the shell script code ysrikanth Solaris 3 11-22-2008 12:38 PM



All times are GMT -4. The time now is 04:36 AM.