#!/bin/sh # Reset the Internal Field Separator resetIFS() { IFS=' ' } # Check if a string is in a list of space-separated string # # Arguments: # $1: the string to be checked # $2: the string list # # Return: # 0 (true): if the string is in the list # 1 (false): if the string is not in the list stringInList() { resetIFS for stringInList_listItem in $2; do if test "$1" = "$stringInList_listItem"; then return 0 fi done return 1 } # Return the strings that are present in two space-separated strings # # Arguments: # $1: the first space-separated string # $2: the first second-separated string # # Output: # The space-separated list of strings that are present in both lists commonElements() { commonElements_result='' resetIFS for commonElements_inA in $1; do if stringInList "$commonElements_inA" "$2"; then if test -z "$commonElements_result"; then commonElements_result="$commonElements_inA" else commonElements_result="$commonElements_result $commonElements_inA" fi fi done echo "$commonElements_result" }