why formLayout not found? - maya

string $formEX = `formLayout -numberOfDivisions 100 -p formLayout5`;
string $buttonEX = `button -l "button" -h 26`;
$formLayout8 = `formLayout -q -ca formLayout5`;
formLayout -e
-attachForm $buttonTime "top" 0
-attachForm $buttonTime "left" 0
-attachForm $buttonTime "bottom" 0
-attachForm $buttonTime "right" 0
$formEX
////////////////////////////execute till here//////////////////////////////////
/////////////////seperately execute this code/////////////////////
formLayout -e -attachForm $formTime "top" 0
-attachForm $formTime "left" 0
formLayout8
i am not understanding why code giving error? how to get ui element name?"formlayout8" is working with query
flag for examle like(formLayout -q -p formLayout8)but i cant merge my own formLayout with that existing one...any help appreciated....
thank you.

Please see the example on the mel documentation for a simple breakdown here.
You've made it a little difficult for us to debug because you're missing code.
If you format your code like the linked example, it will be much easy to follow, mel UI creating has never been a joy to write.
Your problem lies with you're trying to attach items to a form name formLayout8, where you've defined it as a variable $formLayout8, simply add the dollar sign before the name, and remember to close off the line with a semi-colon;
Without knowing what the rest of your code looks like, i can only assume that the errors you have in your code already, is the problem:
string $formEX = `formLayout -numberOfDivisions 100 -p formLayout5`;
string $buttonEX = `button -l "button" -h 26`;
$formLayout8 = `formLayout -q -ca formLayout5`;
formLayout -edit
-attachForm $buttonTime "top" 0
-attachForm $buttonTime "left" 0
-attachForm $buttonTime "bottom" 0
-attachForm $buttonTime "right" 0
$formEX;
formLayout -edit
-attachForm $formTime "top" 0
-attachForm $formTime "left" 0
$formLayout8;
If you have a form with a name of formLayout8, similar to:
string $form = `formLayout -numberOfDivisions 100 "formLayout8"`;
Simply just double quote around the name:
formLayout -edit
-attachForm $formTime "top" 0
-attachForm $formTime "left" 0
"formLayout8";

Related

KSH Verifying if a number exist in a list

I have a list of numbers, around 200 and at the beginning of my ksh I want to verify if parameter 1 is one of this numbers.
I solved this with a big if, but I think that a more elegant solution must exist.
In example, something like this, but in ksh
if $1 in (50, 28, 500, 700, 1, 47) then
do what I want
else
exit
end if
Any idea to start working?
Thanks.
Luis
I found the solution
case $1 in ( 50 | 28 | 500 | 700 | 1 | 47 )
echo ¨Found!¨
;;
*)
echo ¨NOT found!¨
;;
esac
Thanks!
The case statement works for short lists if the list changes or is long, that can get ugly in a hurry. Another idea is to use an associative array. I set up a list of 100 random numbers in the file rand.txt and ran this script to check for numbers on the list:
typeset -A numList
for num in $( < rand.txt )
do
numList[$num]=$num
done
if [[ -n ${numList[$1]} ]]
then
echo "do what I want"
else
echo 'not interesting'
fi
If you don't want a separate file with the numbers, this also works:
typeset -A numList
(
cat <<EOF
72
107
104
82
20
21
EOF
) | while read num
do
numList[$num]=$num
done
if [[ -n ${numList[$1]} ]]
then
echo "do what I want"
else
echo 'not interesting'
fi
These also work on bash.

Variable value in variable name using bash

I have a bash script that calculates the number of Compiz Viewports then dependent on that number defines variable values for each viewport to allow jumping to that viewport.
wmctrl -d
0 * DG: 19200x2160 VP: 0,0 WA: 0,38 3840x2084 Workspace 1
This output tells me the total viewport area is 19200
wmctrl -d | awk '{print $4}' | sed -e 's/x..*//'
19200
and each viewport is 3840 in size
wmctrl -d | awk '{print $9}' | sed -e 's/x..*//'
3840
therefore the total number of viewports is 19200/3840 or in this instance 5
what I now want to do is populate a number of variables (maximum total number of viewports) with the value of that viewport location. In the example above there are 5 viewports so:-
VIEWPORT(1 2 3 4 5)
3840, 7680, 11520, 15360, 19200
then using
wmctrl -o $VIEWPORT(x),0
(Above edited to show that $VIEWPORT(x) is actually a variable, I didn't make it clear originally, so the actual value of say $VIEWPORT3 would be 11520, sorry my bad in the original question)
takes us to that viewport.
The thing I'm struggling with is that as the number of variables required varies on the the number of viewports identified in the beginning.
I've read a bit about arrays but can't see an easy way to use an array to make this work (my lack of familiarity).
So any suggestions regarding how to do this would be appreciated.
Maybe something like:
while read -r size
do
echo "wmctrl -o VIEWPORT($size),0"
done< <(wmctrl -d | awk -F'[ x]' '{for(i = $10; i <= $4; i += $10)print i}')
Remove the echo and quotes once your happy
Bash arrays aren't too complex. If you want to have the variables for use later;
#Total Viewport Area
totview=$(wmctrl -d | awk '{print $4}' | sed -e 's/x..*//')
#Each Viewport size
viewsize=$(wmctrl -d | awk '{print $9}' | sed -e 's/x..*//')
#Loop as many times as totview/viewsize equals
for ((i=1;$i<=$(($totview / $viewsize));i++)); do
array[$i]=$(($viewsize * $i)) #sets the array entry at index $i to the next viewsize. Remember that $i increases by one every loop
done
Now you'll have a variable number of variables, each named array[#]. In your example, you'd have "array[1]" through "array[5]", with the numbers 3840, 7680, 11520, 15360 and 19200. Later on, when you want to use the variables, you can do:
wmctrl -o VIEWPORT(${array[$x]}),0
to get a specific viewport, with $x being a number from one to five. You can fetch the number of indexes in array with ${#array[*]}. In this case, echo ${#array[*]} would print 5.

Split string after matching the word in shell

I have a csv file in that values are like:
Wt-Do-U-Do-Wit-The-Black,black
Yay-Its-Your-Birthday-Black,black
You-Are-My-Sunshine-Happy-Birthday-Red,red
You-Are-Special-Navy-Blue,navy-blue
You-Dont-Look-A-Day-Over-Fabulous-Green,green
You-My-Friend-Are-Ridiculously-Fabulous-Happy-Birthday-Pink,pink
I want to split each string before colour name comes.For Ex:
str1=Wt-Do-U-Do-Wit-The
str1=Yay-Its-Your-Birthday
str1=You-Are-My-Sunshine-Happy-Birthday
str1=You-Are-Special
str1=You-Dont-Look-A-Day-Over-Fabulous
str1=You-My-Friend-Are-Fabulous-Happy-Birthday
For searching the string i am using :-
if [ "$string" == *"Black"* ] && [ "$string" == *"White"* ] ; then
echo "It's there!"
else
echo "SOrry"
fi
It is searching fine. But how can I split the string?
Another way I used :
colour_arr[0]='Red'
colour_arr[1]='Black'
colour_arr[2]='Navy-Blue'
colour_arr[3]='White'
inarray=$(echo ${colour_arr[#]} | grep -o "$string" | wc -w)
echo "$inarray"
But this is not working.
you can use sed; inspired from this answer
I simplified the problem a little since you parsed the strings already correctly; using this input file:
This is red colour
Ball is black colour
some more words before red and more after
for the second part of the string; starting with the color name:
sed -n -e 's/^.*\(\(red\|black\).*\)/\1/p' test
gives:
red colour
black colour
red and more after
and
sed -n -e 's/\(^.*\)\(\(red\|black\).*\)/\1/p' test
gives:
This is
Ball is
some more words before
I won't explain all the options; since they are well explained in the answer I referred to. You can use sed on a bash variable using:
leftpart=$(sed -n -e 's/\(^.*\)\(\(red\|black\).*\)/\1/p' <<< $INPUT_STRING)
EDIT after the OP changed the input format:
my answer still applies; just replace red with Red. The rest applies the same.
For your new input
Input
$ cat f2
Wt-Do-U-Do-Wit-The-Black,black
Yay-Its-Your-Birthday-Black,black
You-Are-My-Sunshine-Happy-Birthday-Red,red S
You-Are-Special-Navy-Blue,navy-blue
You-Dont-Look-A-Day-Over-Fabulous-Green,green
You-My-Friend-Are-Ridiculously-Fabulous-Happy-Birthday-Pink,pink
Output ( Using gawk )
$ awk 'BEGIN{IGNORECASE=1;FS="[ ,]";OFS=","}match($1,$2){print "str1="substr($1,1,RSTART-2)}' f2
str1=Wt-Do-U-Do-Wit-The
str1=Yay-Its-Your-Birthday
str1=You-Are-My-Sunshine-Happy-Birthday
str1=You-Are-Special
str1=You-Dont-Look-A-Day-Over-Fabulous
str1=You-My-Friend-Are-Ridiculously-Fabulous-Happy-Birthday
For your old input
Input
$ cat f
"This is red colour",red
"Ball is black colour",black
"Tshirt is white colour",white
"Shoes are blue colour",blue
"This is green colour",green
Output
$ awk 'BEGIN{FS=OFS=","}{gsub(/"/,"");match($1,$2);print "str1="substr($1,1,RSTART-1),"str2=" substr($1,RSTART) }' f
str1=This is ,str2=red colour
str1=Ball is ,str2=black colour
str1=Tshirt is ,str2=white colour
str1=Shoes are ,str2=blue colour
str1=This is ,str2=green colour
OneLiner using awk (gnu for IGNORECASE)
awk -F ',' 'BEGIN{IGNORECASE=1}{sub("-"$NF"$","",$1);print "str1="$1}' YourFile
Self commented code
awk -F ',' '# sepeartor of field is coma
# before first line
BEGIN{
# define case compair behaviour (ignoring the case)
IGNORECASE=1
}
# for each line
{
# substitute the pattern ( minus than field 2 content, so the color, at the end) in fields 1 by "" (remove)
sub( "-" $NF "$", "", $1)
# print the new content of filed 1 with str1= before
print "str1="$1
}' YourFile
Based on your comments, you need color out of first "dashed" field , not the value of the second field (comma separated).
If color in this first "dashed" field is always the last string (dash separated), you can simply use
a="You-Are-My-Sunshine-Happy-Birthday-Red" ; awk -F- '{print $NF}' <<<"$a"
PS: You can isolate the first field of the whole line with cut or awk:
awk -f, '{print $1}' <<<"$fileline" or cut -d, -f1 <<<"$fileline"
You can combine above two to achieve what you need.
Keep it simple:
$< input.txt
Wt-Do-U-Do-Wit-The-Black,black
Yay-Its-Your-Birthday-Black,black
You-Are-My-Sunshine-Happy-Birthday-Red,red
You-Are-Special-Navy-Blue,navy-blue
You-Dont-Look-A-Day-Over-Fabulous-Green,green
You-My-Friend-Are-Ridiculously-Fabulous-Happy-Birthday-Pink,pink
$sed -E 's/(-[^-]+)(,.*)/\2/g' input.txt
Wt-Do-U-Do-Wit-The,black
Yay-Its-Your-Birthday,black
You-Are-My-Sunshine-Happy-Birthday,red
You-Are-Special-Navy,navy-blue
You-Dont-Look-A-Day-Over-Fabulous,green
You-My-Friend-Are-Ridiculously-Fabulous-Happy-Birthday,pink
(Note: on my OS, OSX, sed -E is for extended regex.)

removing all files except a group in bash

I want to remove all files, but the one that I have in a list. Let's say for the sake of argument that these files are in a array. The array only contains the last three digits of the file, without the extension. all files are extension jpg. This is what I've been trying to do. Files name are in the form of GEDC1227.JPG
pics=(227 222 231 248 252 253 255 272 274 278)
for line in *; do
for j in $pics[#]; do
[[ ${line:5:3} == $j ]] && break
done
rm $line
done
I'm aware that there are easier ways to accomplish this; however, I'm kind of intrigue, why my algorithm is not working. I think is because is getting to the rm $line every time, and I haven't found a way to bypass that line when test condition is true (which is for files that I don't want remove)
You need to continue twice i.e. resume the next iteration of the outer loop:
pics=(227 222 231 248 252 253 255 272 274 278)
for line in *; do
for j in "${pics[#]}"; do
[[ ${line:5:3} == "$j" ]] && continue 2
done
rm -- "$line"
done
Adding quotes to ${pics[#]}. Not really necessary but it's a good practice to prevent word splitting when elements expand.
Quoted second argument to == to prevent interpreting it as a glob pattern.
Added -- to rm to prevent trying to read files starting with - as option and cause an error to rm.
This alternative solution may also apply but needs testing:
shopt -s extglob
shopt -s nullglob
echo rm -- ?????!(227|222|231|248|252|253|255|272|274|278)*
Remove echo if found correct.
continue from bash manual:
continue: continue [n]
Resume for, while, or until loops.
Resumes the next iteration of the enclosing FOR, WHILE or UNTIL loop.
If N is specified, resumes the Nth enclosing loop.
I would also consider doing this without a loop at all.
Something like this can be made to run in parallel and won't choke (like a glob might) if there are many files in the directory.
pics=( 227 222 231 248 252 253 255 272 274 278 )
# convert the array to a regex that matches GEDC.227\.JPG$|GEDC.222\.JPG$|...
keep="$( echo "GEDC.${pics[#]}\\.JPG$" | sed -e 's: :\\.JPG$|GEDC.:g' )"
find -type f -iname GEDC*.jpg \
| egrep -v "$keep" \
| xargs rm

Perl reading file, printing unique value from a column

I am new to perl, and i'd like to achieve the following with perl.
I have a file which contain the following data:
/dev/hda1 /boot ext3 rw 0 0
/dev/hda1 /boot ext3 rw 0 0
I'd like to extract the second field from the file and print unique values only. My desired output for this example is, the program should print :
ext3
also if i have several different filesystem, it should print in on the same line.
I have tried many piece of code but am left stuck.
Thank you
If you prefer awk:
$ cat file
/dev/hda1 /boot ext3 rw 0 0
/dev/hda1 /boot ext3 rw 0 0
$ awk '!seen[$3]++{print $3}' file
ext3
OR , using cut:
$ cut -d" " -f3 file | sort | uniq # or use just sort -u if your version supports it
ext3
Here is perl solution:
$ perl -lane 'print $F[2] unless $seen{$F[2]}++' file
ext3
Here is the perl command line options explanation (from perl -h):
l: enable line ending processing, specifies line terminator
a: autosplit mode with -n or -p (splits $_ into #F)
n: assume "while (<>) { ... }" loop around program
e: one line of program (several -e's allowed, omit programfile)
For a better explanation around these option, please refer: https://blogs.oracle.com/ksplice/entry/the_top_10_tricks_of
#!/usr/bin/perl
my %hash ;
while (<>) {
if (/\s*[^\s]+\s+[^\s]+\s+([^\s]+)\s+.*/) {
$hash{$1}=1;
}
}
print join("\n",keys(%hash))."\n";
Usage:
./<prog-name>.pl file1 fil2 ....
perl -anE '$s{$F[2]}++ }{say for keys %s' file
or
perl -anE '$s{$_}++ or say for $F[2]' file

Resources