Hello
I have a string that may or may not have 4 dots.
The string is actualy a file within a folder, where multiple files are located.
Files may look like:
[root@dell nas]# ls *
creds:
192.168.10.110 someserver
shares:
192.168.10.110.Public someserver.sharefolder
[root@dell nas]#
I want to fill 2 variables, $SERVER and $SHARE.
Obviously either '127.0.0.1' or 'servername' should be in $SERVER, while 'sharename' should become $SHARE.
Maybe i could just use another symbol than a dot as filename seperator between $SERVER and $SHARE?
But i nether want to use '-' nor '_' as they might occour in a share- or servername (right?), while i'm not sure how a coma as part of a filename works out.
Please note, the relevant code blocks are the function and the 'else' part.
GetServerShare() { # SERVER | SHARE
# Pass either Server- or Sharename
# Returns SERVER/SHARE (list if multiple)
# Look if ITEM is SERVER
servers="$(ls $CONFIG_CREDS|grep $1)"
# Look if ITEM is SHARE
shares="$(ls $CONFIG_SHARES|grep $1)"
# The grep \. should print out only server.share item,
# given the assumption the user has set them up already,
# while grep $1 will reduce the list to match provided argument/string
output="$(printf $servers $shares |grep \.|grep $1)"
for ITEM in $output;do
printf "$ITEM\n" # Need code here
done
}
....... other code ....
else if ( [ $mode = mount ] && [ ! "" = "$(echo $1|grep '/')" ] ) || \
( [ ! $mode = mount ] && [ ! "" = "$(echo $2|grep '/')" ] )
then # Adress contains slashes, thus contains server & share info
if [ ! $mode = mount ] #|| [ $doEdit ] || [ $doUnmount ]
then export SERVER=$(printf "$2"|sed s,'/',' ',g|awk '{print $1}') \
SHARE=$(printf "$2"|sed s,'/',' ',g|awk '{print $2}')
else export SERVER=$(printf "$1"|sed s,'/',' ',g|awk '{print $1}') \
SHARE=$(printf "$1"|sed s,'/',' ',g|awk '{print $2}')
fi
else # Its just the IP/NAS or the Share name
STR=$(GetServerShare $server_info)
# This export needs to be changed too,
# if i'd change the sed from / to '.' i'd get only 127 as SERVER,
# but it would work if the servername was used (but would required changes at /etc/hosts
export SERVER=$(printf "$STR"|sed s,'/',' ',g|awk '{print $1}') \
SHARE=$(printf "$STR"|sed s,'/',' ',g|awk '{print $2}')
echo $SERVER # DEBUG - debug:: 127.0.0.1.Share
echo $SHARE # DEBUG - debug:: EMPTY
fi
export CONFIG_NAS="$CONFIG_CREDS/$SERVER" \
CONFIG_SHARE="$CONFIG_SHARES/$SERVER.$SHARE"
.....
# Current 'fix' as the above 'else' in combination with export produces a trailing dot
[ "${CONFIG_SHARE:${#CONFIG_SHARE}-1}" = "." ] && \
export CONFIG_SHARE="${CONFIG_SHARE%?}"
As of now, within that elseblock, i need to split up $SERVER (127.0.0.1.SHARENAME) and make SHARENAME the $SHARE variable, which is currently empty.
Any ideas?
Thank you in advance
EDIT PS:
Always right after posting an idea pops up
I could just replace the dots with spaces (using sed), and then check how many words there are, and use the last word (like for word in line,do SHARE=$word;done
) to be used as Variable $SHARE
No better solution comes to mind.
---------- Post updated at 16:13 ---------- Previous update was at 15:35 ----------
/solved
The loop works, though its not elegant, but once all is working, the scripts needs another rewrite anyway.