Posted on 01-08-2014 03:07 AM
I Have the following problem:
I have to create a script that does nothing more than copy a folder from destination A into the home-directory of the actually logged-in user. The script should be executed by SelfService.
The idea is to have the source folder as parameter value $4, the actually logged-in user as parameter value $5 and the destination as parameter value $6.
I use the following values:
$4 = /private/tmp/Folder_A
$5 = `ls -l /dev/console | cut -d " " -f 4`
$6 = `/Users/$(echo "$(ls -l /dev/console | cut -d " " -f 4)")/Documents/`
If I test the script in a terminal window it works fine, but if I execute it by SelfService, the values in $5 and $6 are not executed as values but as text instead. The result is an error message: 'Script result: cp: `/Users/$(echo "$(ls -l /dev/console | cut -d " " -f 4)")/Documents/`: No such file or directory…' Value $4 works fine. I already tried to cut the leading "``" without success.
Where is my mistake? Thank you very much for your advice!
Solved! Go to Solution.
Posted on 01-08-2014 09:20 AM
So to be clear, are you setting these variables up directly into the script, or are you trying to pass them to the script from Casper/policy? If you're trying the latter, first off, I don't believe Casper's script parameters can accept anything other than strings. I've never tried using a command as a passed parameter, but I don't think that works. If I'm correct, that would jive with why your $4 works and $5, $6 don't. The first is a string, the latter two are commands.
Secondly, I can't think of any reason why you would want to pass those 2 commands as parameters. They should simply be set up as variables that get created at the time the script runs, using some logical name for them. Maybe that's what you're actually trying to do. I don't know. If so, reserve $4 through $11 for parameters that Casper passes down to the script. Don't try using those as variable names.
So for example, add these to your script:
currentUser=`ls -l /dev/console | cut -d " " -f 4`
destFolder=`/Users/$(echo "$(ls -l /dev/console | cut -d " " -f 4)")/Documents/`
Or better yet, for the second one, just do:
destFolder=`/Users/$currentUser/Documents/`
You're already capturing the current username. Just use that variable to get the path to Documents in your second variable.
So in short, you can assign $4 to your source directory and pass that to the script and have the other two variables being generated directly in the script when it runs.
Make sense?
Posted on 01-08-2014 09:20 AM
So to be clear, are you setting these variables up directly into the script, or are you trying to pass them to the script from Casper/policy? If you're trying the latter, first off, I don't believe Casper's script parameters can accept anything other than strings. I've never tried using a command as a passed parameter, but I don't think that works. If I'm correct, that would jive with why your $4 works and $5, $6 don't. The first is a string, the latter two are commands.
Secondly, I can't think of any reason why you would want to pass those 2 commands as parameters. They should simply be set up as variables that get created at the time the script runs, using some logical name for them. Maybe that's what you're actually trying to do. I don't know. If so, reserve $4 through $11 for parameters that Casper passes down to the script. Don't try using those as variable names.
So for example, add these to your script:
currentUser=`ls -l /dev/console | cut -d " " -f 4`
destFolder=`/Users/$(echo "$(ls -l /dev/console | cut -d " " -f 4)")/Documents/`
Or better yet, for the second one, just do:
destFolder=`/Users/$currentUser/Documents/`
You're already capturing the current username. Just use that variable to get the path to Documents in your second variable.
So in short, you can assign $4 to your source directory and pass that to the script and have the other two variables being generated directly in the script when it runs.
Make sense?
Posted on 01-13-2014 01:49 AM
Thank you very much for your answer!
Of course it does make sense, and I would have done it your way, too.
But I might have to add that it was my task to create a reusable script as simple as possible, something like 'cp -R $4 $6'. That's why I tried it this way not knowing if it it would work or not.