Determine if variable is the Server component of UNC

Hi all,

Using sh/csh, unfortunately shell scripts are not my strong suit.

Trying to write a script that gets called from a program for pre-processing.
The program passes individual components of a UNC (//server/path1/path2/filename).

Thus the unc path of: //server/path1/path2/filename, is 4 calls to myscript with each call having a single parameter of:
//server
//server/path1
//server/path1/path2
//server/path1/path2/filename

I need to be able to determine if the current component is the Server component or not. ie: //server not //server/blah.

Figured that would be easy ... Something simple like:

RESULT=`echo "$1" | sed -e '/^\/\/.+\//p'`
if [ ! "$RESULT" ]; then
  echo "Server Component"
else
  echo "Path components"
fi

should get it done, Obviously I was wrong.

How can I go about this or what am I doing wrong?

The Server component will always begin with two slashes followed by a name, no trailing slash.
Non server components will always have two leading slashes, the server name followed by a slash ... and more.
If it does not start with two leading slashes it should be completely ignored.

Thanks

-Enjoy
fh : )_~

Could this help you ?
Input file

$ cat server.txt
//server
//server/path1
//server/path1/path2
//server/path1/path2/filename

Invocation

$ awk -F"/" 'NF==3{print "Server Component: "$0}
NF>3{ print "Path components: "$0}' server.txt

Output

Server Component: //server
Path components: //server/path1
Path components: //server/path1/path2
Path components: //server/path1/path2/filename

If you have Ruby

r=$(echo "//server" | ruby -e 's=gets; puts s[/^\/\//] && s.count("/")==2? 0:1 ')
# check if $r == 0

Assuming that file does not contain blank line

$ awk  '{print gsub(/\//,"&") == 2 ? "Server Component: "$0 : "Path components: "$0 }' file
$ awk  '{print /server$/ ? "Server Component: "$0 : "Path components: "$0 }' file

Was I not clear.

Ruby and AWK are out ... not available.
That is the reason for my second line in original post. sh/csh!
sed is available.

I am not Reading or Processing a file ...

A Single parameter is passed to myscript, that parameter will contain ONE part of a path.
In myscript, it is referenced by $1.
I need to know if that one component (in $1) is the server part or not (ie: //server and not //server/blab) ... Thats all!

Obviously this will be used for flow control. as per my example in the original post.

Thanks

-Enjoy
fh : )_~

Here is csh code

#!/bin/csh

foreach line ("`cat filename`")
    if (`echo "$line" | tr -dc '/' | wc -c` == 2) then
        echo "Server Component : "$line
    else
        echo "Path components : "$line
    endif
end

Even sh should have "substring processing parameter expansion" and "short circuit list operators". So try:

[ "/" == ${1%/*} ] && echo "server" || echo "path"
1 Like

RudiC,

Absolute sweet perfection ... Much Thanks!

Never even thought of using expansion, I was way over thinking it.
I have not dissected it yet to fully understand, I will.

Took me a minute to figure out the unary operator expected error, it still worked, just gave an error.

Quoting ${1%/*} like so "${1%/*}" solved that.

My final result: if [ "/" != "${1%/*}" -a ! -d "$1" ]; then

Thanks y'all

-Enjoy
fh : )_~