Determine if variable is the Server component of UNC


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Determine if variable is the Server component of UNC
# 1  
Old 12-30-2013
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:
Code:
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 : )_~
# 2  
Old 12-30-2013
Could this help you ?
Input file
Code:
$ cat server.txt
//server
//server/path1
//server/path1/path2
//server/path1/path2/filename

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

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

# 3  
Old 12-30-2013
If you have Ruby
Code:
r=$(echo "//server" | ruby -e 's=gets; puts s[/^\/\//] && s.count("/")==2? 0:1 ')
# check if $r == 0

# 4  
Old 12-30-2013
Assuming that file does not contain blank line

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

Code:
$ awk  '{print /server$/ ? "Server Component: "$0 : "Path components: "$0 }' file


Last edited by Akshay Hegde; 12-30-2013 at 06:04 AM..
# 5  
Old 12-30-2013
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 : )_~
# 6  
Old 12-30-2013
Here is csh code

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

# 7  
Old 12-30-2013
Even sh should have "substring processing parameter expansion" and "short circuit list operators". So try:
Code:
[ "/" == ${1%/*} ] && echo "server" || echo "path"

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to determine server IP

Need help with the script, I am trying to include this script as part of kickstart profile. based of the host's IP address, in this case if the host is IP starting with 10.10.3.* or 10.10.6.*, I will be pushing appropriate routing file from my web server. I validate host IP from nslookup. ... (3 Replies)
Discussion started by: bobby320
3 Replies

2. Web Development

Script to determine which web server at ip addresses

how do we determine if ip addresses are hosting IIS version 7.x or Apache 2.2.x. ? (3 Replies)
Discussion started by: NameSake
3 Replies

3. Red Hat

How to determine share name of Linux server?

Hi, How to determine share name of Linux server ? OS version is RHL 6.5 Regards, Maddy (11 Replies)
Discussion started by: Maddy123
11 Replies

4. Shell Programming and Scripting

To determine the File Sytem Usage on Multiple UNIX server

Hello All :) I want to write a shell script to find the file system usage on multiple UNIX servers. Commands: df -g fsJCAPS Below script works fine and it displays results on terminal/console. I want to store /redirect output on to local server from where I'm running the script. ... (3 Replies)
Discussion started by: Mohammad Nawaz
3 Replies

5. Shell Programming and Scripting

Determine a shell variable exists in file?

Hi Folks, Trying to build up a script that will lookup a username invoked as: ./buildscript.sh <username> This should take <username> and look it up in <username_file> and prepare for further processing. Here is the snippet that isn't working just right: user=$1 if ]; then echo... (1 Reply)
Discussion started by: gdenton
1 Replies

6. Shell Programming and Scripting

Parse file name out of UNC path

Hello, I searched the forums and didn't see a situation like this: I cannot figure out how to parse out just the file name from the full path. The path looks like this: \\foo\bar\filename.ext I don't think something like 'cut' will work so I tried to whip up a regex but couldn't get it... (12 Replies)
Discussion started by: bytesnoop
12 Replies

7. Shell Programming and Scripting

To verify status of particular Component(Siebel Server srvrmgr)

Hi , I want to connect srver to pertuculat mode(i.e.srvrmanger)and after that I want to verify status of perticular component(i.e.CommOutboundMgr) For that I have created following script bout after 3rd line it is not executing 4th linei.e. list comp CommOutboundMgr. cd... (2 Replies)
Discussion started by: vivek1489
2 Replies

8. IP Networking

How to Determine client's DNS server Ip

Is there a way for a server to determine client's DNS ip? I have an application that logs client's IP but in certain cases its desirable to know their DNS too (1 Reply)
Discussion started by: vickylife
1 Replies

9. Shell Programming and Scripting

Using Perl to connect UNC Filepaths on Domain

I'm trying to use Perl on Windows (Doh!) to connect to a folder on a Domain Controller via UNC. Right now, I have perl -e "`runas /user:DOMAIN\\Username dir \\\\SERVER\\d\$\\Path`" This does not seem to connect nor does it prompt for password. Should I try throwing it into a script and... (0 Replies)
Discussion started by: adelsin
0 Replies
Login or Register to Ask a Question