ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm simply not familiar with the intricacies of C shell scripting.
How do I change the contents of a variable through a regular expression? Assuming I want to simplify an absolute pathname by using tilde notation, how would this be done within a C shell script?
In Perl, this would be done similarly to:
Code:
my $s = `pwd`;
$s =~ s/^\Q$ENV{HOME}/~/;
It appears that tools like grep or sed all work on files, so I'm confused given that the value I want to translate is already contained in a variable.
The form of the assignment statement will vary from Bourne shell relatives (e.g. bash) to cshell (e.g. tcsh), but as Tinkster has written the key concept is to use echo. For example:
Code:
#!/bin/tcsh
# @(#) s1 Demonstrate set, setenv in tcsh.
echo
echo "$version" | sed 's/options.*//'
echo " Using shell $SHELL"
set script = "hello"
set SCRIPT="world"
setenv VAR " This will be an environment variable."
echo
echo " script is ${script}, SCRIPT is ${SCRIPT}"
echo " ( evaluating $script is same as ${script} )"
echo " The environment variable is :${VAR}:"
env | grep VAR
echo
echo " Argument 0 is $0"
set program = `basename $0`
echo " The result of basename on argument 0 is $program"
echo " All arguments from argv are $argv"
exit 0
producing:
Code:
% ./s1 one two three
tcsh 6.13.00 (Astron) 2004-05-19 (i386-intel-linux)
Using shell /bin/tcsh
script is hello, SCRIPT is world
( evaluating hello is same as hello )
The environment variable is : This will be an environment variable.:
VAR= This will be an environment variable.
Argument 0 is ./s1
The result of basename on argument 0 is s1
All arguments from argv are one two three
As a follow-up, the later Bourne family shells can do some processing directly on variables:
Quote:
${parameter/pattern/string}
${parameter//pattern/string}
The pattern is expanded to produce a pattern just as in pathname
expansion. Parameter is expanded and the longest match of pat-
tern against its value is replaced with string. In the first
form, only the first match is replaced. The second form causes
all matches of pattern to be replaced with string. If pattern
begins with #, it must match at the beginning of the expanded
value of parameter. If pattern begins with %, it must match at
the end of the expanded value of parameter. If string is null,
matches of pattern are deleted and the / following pattern may
be omitted. If parameter is @ or *, the substitution operation
is applied to each positional parameter in turn, and the expan-
sion is the resultant list. If parameter is an array variable
subscripted with @ or *, the substitution operation is applied
to each member of the array in turn, and the expansion is the
resultant list.
-- man bash on patterns
For example:
Code:
#!/bin/sh
# @(#) s1 Demonstrate patterns and variables.
set -o nounset
echo
echo "GNU bash $BASH_VERSION" >&2
echo
t1="hello bad world"
echo " The value of t1 is :$t1:"
echo " Result of t1 processed :${t1/?bad?/ GOOD }:"
exit 0
producing:
Code:
% ./s1
GNU bash 2.05b.0(1)-release
The value of t1 is :hello bad world:
Result of t1 processed :hello GOOD world:
This is but one of the many reasons that most people prefer to write shell scripts in the Bourne shell family ... cheers, makyo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.