LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-10-2010, 04:54 PM   #1
WinnieNicklaus
LQ Newbie
 
Registered: Nov 2010
Posts: 5

Rep: Reputation: 0
Passing on command line switches to other programs


On our server we have a certain directory, say /storage, that contains many large files. They are all compressed (gzip). Many of our users are not computer-savvy, and so when one of these files is needed, they will copy it to their own directory. Consequently, we have multiple terabytes of duplicate data. I'd like to enforce an alias whereby if someone tries to use cp on a file from /storage, they will instead create a symbolic link. My idea was something like:

alias cp='cp.storage'

File cp.storage:
#!/bin/sh

truePath=$(readlink -f "$1")

if [ $(expr match "$truePath" "/storage") -gt 0 ]
then
gzip -rd "$1" 2> /dev/null
ln -s "$truePath" "$2"
else
/bin/cp "$1" "$2"
fi

The conditional checks whether the file being copied begins with "/storage".

The problem with this is that if someone wanted to use cp with any options on a file not in /storage, those options would be obliterated. Can someone guide me as to a good way to accomplish this? Either a way to get the options from cp into cp.storage, or another approach not using alias this way. Everyone will be using bash.
 
Old 11-10-2010, 05:32 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Here is a way to reference all of the arguments passed to a bash script.
http://www.tldp.org/LDP/abs/html/int...s.html#ARGLIST

So you would probably want to do something like
Code:
cp $@
I believe that will pass all of the original arguments to the cp command.

Be sure to define the alias in an export statement so that the alias will be defined in child processes.

Your code has a bit of a problem though. If it is called with typical parameters for cp your code chokes.

Code:
cp -i -v /storage .
readlink: invalid option -- 'i'
Try `readlink --help' for more information.
Actually I edited your code for testing as follows.
Code:
#!/bin/sh

truePath=$(readlink -f "$1")

if [ $(expr match "$truePath" "/storage") -gt 0 ]
then
     echo "create link"
else
     echo "real copy"
fi

exit
I found that it won't work for
Code:
cp /storage/hammer .
real copy
It looks like you've got a bit of work ahead of you.

Last edited by stress_junkie; 11-10-2010 at 05:48 PM.
 
Old 11-11-2010, 03:04 PM   #3
WinnieNicklaus
LQ Newbie
 
Registered: Nov 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Well, I didn't read the manpage of cp closely enough. There's an option -s (--symbolic-link) to create symbolic links instead of copying. Thanks for the tip about $@ -- that's what I needed! I have a script that should work now. (By the way, I think the reason your example "cp /storage/hammer ." didn't work is because the file didn't exist, so readlink returned "", which doesn't match "/storage". This shouldn't cause any problems, as far as I can see. If a source file doesn't exist, then regular cp can handle that. If the target doesn't exist... well, users aren't allowed to write to /storage.)

I scan all of the arguments to see if they are existing files. If so, I see if they begin with "/storage". If they do, then I set a variable "link" to 1. After scanning the arguments, if link is equal to 1, then I use cp and add the symbolic link option -s. Otherwise, I go ahead with the copy.

Code:
#!/bin/sh

link=0

for arg in $@
do
  if [ -a $arg ]
      then

      truePath=$(readlink -f "$arg")
      if [ $(expr match "$truePath" "/storage") -gt 0 ]
          then
          link=1
      fi
  fi
done

if [ $link -eq 1 ]
    then
    cp -s $@
else
    cp $@
fi
Thanks for your help. I'm putting this up in case anyone has a similar problem they want to tackle. Let me know if you see any issues with what I came up with.

Last edited by WinnieNicklaus; 11-11-2010 at 03:28 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Spaces in command line switches ProgrammerTim Linux - Newbie 2 10-06-2010 04:54 PM
Passing command line arguments jason_m Programming 4 08-26-2010 10:36 PM
Passing SSH a password through command line info1686 Linux - General 5 08-05-2010 12:17 AM
Passing directory name as a command line argument adilwaheed Linux - Newbie 4 03-12-2009 03:01 PM
passing command line to a BASH function GSMD Programming 16 04-21-2007 03:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration