Can't get bash case statement to set variable
Can somebody point out what I'm doing wrong here?
Code:
case ${WClass} in |
Are you looking for/at $Bkg *after* the script exits? (It's 'gone'!)
Put an echo $Bkg as the last line, after the esac, to 'check'. (I did that, and it 'worked' *inside* that script; but 'not' at shell prompt, after script) http://serverfault.com/questions/336...ript-execution Try: source yourscriptname $ sh z /solid $ echo $Bkg $ source z /solid $ echo $Bkg /solid $ Is this what is happening in you case? Best wishes & welcome to LQ! p.s. Oh, I added as a first line, to test: WClass="Xfe" (not directly related, but lookup export, for *child* processes to see it) and getting deeply obscure: http://stackoverflow.com/questions/4...-calling-shell |
You had {}'s around $WClass. I tried out the code below and it works. Also, for string constants (e.g. 'Pale moon'), I like single quotes better than double quotes but either should work.
Code:
case $WClass in |
I made the suggested changes and still had the problem;
Code:
# Function to check if current window needs solid background Code:
LilyTerm Bkg= Does it matter that this function is called from a while loop? Shouldn't it still echo whatever is inside of the function regardless where it was called from? I apologize if I'm not providing enough information. The last coding I did was in BASIC on an Apple II. So I don't really know what's relevant to the problem. I thought it was just a missing set of brackets or something else with the syntax of the case statement. Obviously, it's getting the WClass information from the function that parses xprop. I think there's something wrong with my system overall. This is not the only problem I'm having with strange behavior right now. This may be just another symptom of the larger issue. In case it makes any difference, here's all the functional code from the script. It's all separate functions at the moment because it's easier for me to figure out little bits at a time. I plan to tie it together more cleanly once I grasp what I'm doing; Code:
# Function to get window info |
So, normally I would just say use the good ole set -xv, however this did not help in this case, so what it was back to not only echoing variables but also including delimiters to make sure what
you have stored :) Update to the following and all will be revealed: Code:
echo "|$WClass| Bkg=$Bkg" and you need to tell bash what the variable name is, like: Code:
echo "start$WClassend" |
Quote:
Code:
| LilyTerm| Bkg= Code:
get_xprop(){ Code:
|LilyTerm| Bkg= |
This works for me:
Code:
base=$(xprop -root | awk '/^_NET_ACTIVE_WINDOW/{print $NF}') |
Quote:
|
No need, really, for all the mawking about with grep/sed/tr/cut/awk!
Code:
#!/bin/bash |
Quote:
*EDIT* WINDOW_ROLE seems to be the most commonly ignored hint. But as you can see in the output I pasted above, even window class can be ignored by some programs. That's why I gathered every bit of information my WM recognizes. |
You would need to go over the data as $5 is not the same 'base' value for me, seeing xprop -root gives a whole screen of data.
As to what is going on, the 'set' command is setting the positional variables 1 to N as if you passed all the arguments to a script and then you reference them by number, 2, 3, 4 & 5 in the example provided. For me the base is the 13th value and here you would again need to use {} to keep the numbers grouped together, ie. ${13}, but gnashley is quite correct that parameter substitution is another good option to stay all in bash without punching out to other commands :) |
Quote:
Code:
_XROOTPMAP_ID(PIXMAP): pixmap id # 0x1c00001 Code:
$ set $(xprop -id $base WM_NAME) |
Glad you are getting into the joys of bash :) Just in case you do not already have it, the link below is one I fully recommend reading through as time permits:
http://mywiki.wooledge.org/TitleIndex |
Quote:
Honestly, I started learning bash because it's always there and I'm always needing to do something trivial with it. I was starting to believe the folks who were telling me that bash is just a phase and I'll grow out of it and start looking for a "real" programming language. But this thread has really opened my eyes. I thought I was getting close to competent. Now I see I haven't even scratched the surface. Yes, I'm excited to see where I can go now that I know there's much more to explore than I had imagined. This is far from a "basic" language. I did want to ask, what is the purpose of the underscores in this line? Code:
IFS=',"' read _ WInstance _ _ WClass <<<$(xprop -id $base WM_CLASS) |
And don't ignore the bash man page - a terrific resource!
Although certainly not tutorial in style, it is actually very readable, accessible and complete - a treasure among man pages! I think a common obstacle for many is simply its length and unfamiliarity. So learn how it is organized and how to navigate quickly to major topics. Read it, one digestible section at a time to gain comfortable familiarity over time. You will be glad you did! |
All times are GMT -5. The time now is 12:18 AM. |