![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| can someone help me with modifying this file | eamani_sun | Shell Programming and Scripting | 2 | 05-22-2008 10:15 AM |
| Need help in modifying the prompt | mahatma | Shell Programming and Scripting | 7 | 08-02-2006 12:45 AM |
| modifying my shell | nico-hellas | Shell Programming and Scripting | 1 | 08-05-2005 06:43 AM |
| modifying the writing of a log... | Nicol | Shell Programming and Scripting | 2 | 04-13-2005 07:05 AM |
| Modifying $PATH variable in /etc/profile | Deepali | UNIX for Dummies Questions & Answers | 3 | 03-12-2001 08:17 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
Modifying a variable value
Hi all, how do i modify a variable's value.
var1='abcd efgh ijkl mnop abcd' how do i get var2 from var1 var2=$(......) $echo var2 abcd efgh ijkl mnop i.e. i have removed a duplicate occurence. or in general how to modify a varible. thanks in advance |
| Forum Sponsor | ||
|
|
|
|||
|
It's not at all clear what concept you are looking for. Perhaps you could elaborate on that a bit. In the meantime, here are some hopefully useful exercises.
Code:
var2=$var1 #copy var1 to var2
var2=${var2%abcd} # trim abcd from end, if present
var2=`echo "$var2" | tr ' ' '\012' | sort | uniq | tr '\012' ' '` # remove duplicate tokens
Perhaps you should read a tutorial on shell programming at this point; there's a lot of things you can do with variable substitution (${var%trim} is but a single example). |
|
|||
|
hi era
I am used to programming in Matlab and C++ and in these languages you can refer to individual members of a variable (array) and compare their values to all the other members using 2 loops. how can i accomplish that in unix. For example var2=$(users) will return the current users but if a user is running two instances of a shell then that username will be returned twice. How can i trim the varible $var2 so that multiple occurrences of usernames are eliminated. i can use a for loop to loop through each member of the varible but then how do i compare it to the rest of the usernames in $var2. I am referring to tutorials as well but most of them jus explain the general syntax of commands. i guess i am not referring to the right ones, will work on tht. |
|
|||
|
In fact my sort | uniq example was not so far off the mark then.
A variable in Bourne classic is just a piece of text; some shells such as bash and ksh have array variables, too. The common trick would be to modify the text so it is in a suitable form by the time you assign it to a variable. Code:
var2=$(users | sort | uniq) |
|
|||
|
Code:
set -- `users` var2=" " for f; do case $var2 in *" "$f" "*) ;; *) var2=" $f$var2";; esac done For convenience, I made it so that $var2 will have one leading and one trailing space. Removing or ignoring those is left as an exercise. The shell is not very good at these things; that's why awk and sed are more or less essential for nearly any moderately complex shell script. (Or use Perl if that helps at all.) Modern shells have additional facilities, including an array variable type, but this approach works even with Bourne Classic. We are stuck with it, and in fact, we kind of like it, in spite of its oddities and quirks. |
|||
| Google The UNIX and Linux Forums |