- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: perl, shell problem with quotes, escaping
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 03:36 AM
03-22-2007 03:36 AM
X_ENV=${X_VECT}00n02 #old line
X_ENV=${X_VECT}23n02 #new line
where 23 is a number found earlier in script.
This works fine on a test .profile :-
export NCODE="23" #set it here for testing
perl -pi.old -e '$NCODE=$ENV{NCODE}; \
-e s/^X_ENV=\$\{X_VECT\}00n02/X_ENV=\$\{X_VECT\}${NCODE}n02/' ./.profile
but to run it on a user's write-protected .profile I do:-
su root -c "perl -pi.old -e '$NCODE=$ENV{NCODE}; \
-e s/^X_ENV=\$\{X_VECT\}00n02/X_ENV=\$\{X_VECT\}${NCODE}n02/' ./profile"
and get errors:-
Backslash found where operator expected at -e line 1, near "X_VECT\"
Backslash found where operator expected at -e line 1, near "X_VECT\"
Can't modify constant item in scalar assignment at -e line 1, near "};"
syntax error at -e line 1, near "X_VECT\"
Execution of -e aborted due to compilation errors.
I've tried escaping and quoting differently, but still can't make it work. The rest of the script needs to be run as a regular user, not root, hence the need for "su root -c".
Any advice would be much appreciated.
Paul
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 03:43 AM
03-22-2007 03:43 AM
Re: perl, shell problem with quotes, escaping
# su root -c 'perl /tmp/_x23.pl'
or
# su root -c 'sh /tmp/_x23.sh'
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 03:50 AM
03-22-2007 03:50 AM
Re: perl, shell problem with quotes, escaping
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 04:13 AM
03-22-2007 04:13 AM
Re: perl, shell problem with quotes, escaping
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 04:36 AM
03-22-2007 04:36 AM
SolutionSince you didn't like Merjin's approach, there is a way to have your cake and eat it too: build your Perl script "on the fly" within a shell script and then invoke it from the shell. You can even store a complete Perl script in a shell variable:
------------------------------------------
#!/usr/bin/sh
build_P1()
{
typeset V1=${1} # shell variables
typeset V2=${2}
shift 2
echo "my \$v1 = ${V1};"
echo "my \$v2 = ${V2};"
echo "print \"One\\n\";"
echo "print \"Two\\n\";"
echo "print \"v1 = \",\$v1,\"\\n\";"
echo "print \"v2 = \",\$v2,\"\\n\";"
return 0
} # build_P1
typeset X=$(build_P1 100 999)
typeset -i STAT=0
perl -e "${X}"
STAT=${?}
exit ${STAT}
-------------------------------------------
This example doesn't perform your task but it does illustrate the necessary techniques. Note that I passed 2 variables into the build_P1 function and these were assigned as Perl variables before being printed. I also printed 2 string constants. Note also that by storing the script in a shell variable, no temporary file is even needed. This is a fairly good technique for build complex awk, sed, Perl, or other shell scripts "on the fly".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 05:13 AM
03-22-2007 05:13 AM
Re: perl, shell problem with quotes, escaping
I would agree with Merijn for the sake of simplicity.
Consider this, though. Instead of mixing Perl and a shell script, since you said that you were "hoping to keep it tidy, all in the one script file", re-write the shell script in Perl in its entirety :-)
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 05:23 AM
03-22-2007 05:23 AM
Re: perl, shell problem with quotes, escaping
the command does exactly what you tell him (as usual): the outer most double quotes -
su root -c ".."
tell the shell to look for substitutions. It doesn't matter that you have single quotes inside this string: they are just literal single quotes.
Simply exchanging single and double quotes would perform the expansion in the shell invocation by the su command. So you have to split the commandline into pieces, so by the last puzzling of su still no expansion is performed. Such you do not have to quote every single $,... but only inject a single quote in the beginning and at the end of the "second level command" via "'". Look at the trace output of
set -x
su root -c 'set -x;print -u2 '"'"'a\t$PATH\n$RANDOM'"'"
You'll see
+ su root -c set -x;print -u2 'a\t$PATH\n$RANDOM'
Password:
+ print -u2 a\t$PATH\n$RANDOM
a $PATH
$RANDOM
Nevertheless: I would prefer Merjin's suggestion.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 08:29 AM
03-22-2007 08:29 AM
Re: perl, shell problem with quotes, escaping
After considering what the extra shell for su was doing I made it work by some extra escapes :-
su root -c "perl -pi.old -e '\$NCODE=\$ENV{NCODE};' \
-e 's/^X_ENV=\\$\{X_VECT\}00n02/X_ENV=\\$\{X_VECT\}${NCODE}n02/' ./.profile"
but this is not easy to read so I have rewritten it the way that A Clay Stephenson suggested. This will make it easier to maintain in the future.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2007 08:34 AM
03-22-2007 08:34 AM