Xdialog menu for mounting USB (Solved)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

Xdialog menu for mounting USB (Solved)

#1 Post by arivas_2005 »

greetings
and excuse my english

I need to create a small menu to mount usb with Xdialog --menubox

Code: Select all

#!/bin/bash
rm -f /temp1
rm -f /temp2

blkid | cut -d":" -f1 | grep -v a | grep -v b>/temp1
IFS=$'\n'
usbArray=($(blkid | cut -d":" -f1 | grep -v a | grep -v b))

# Not work with variable
Xdialog --title "USB unit list"  --menu "Please choose " 45 80 5 ""${usbArray[@]}"" 2>/temp2
# Not work with file
Xdialog --title "USB unit list"  --menu "Please choose " 45 140 5 /temp1 2>/temp2
I need to choose a USB drive in the list (Xdialog menubox) to mount before
How would the code (Xdialog menubox) to work with a variable and file (two cases)?

thanks
Last edited by arivas_2005 on Sun 26 Jun 2016, 13:51, edited 1 time in total.

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#2 Post by don570 »

Did you read the manual

http://murga-linux.com/puppy/viewtopic.php?t=89045

_______________________________________

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#3 Post by arivas_2005 »

Thank you
( newly excuse my english (I use google traslate) )

I checked the texts, but is not xdialog
still I do not understand
I have no programming experience

however I am trying to understand how it works xdialog --menubox

experiment with the following code(code examples comes from google):

Code: Select all

#!/bin/bash
rm -f /temp1
rm -f /temp2

blkid | cut -d":" -f 1 | grep -v a | grep -v b  >/temp1
IFS=$'\n'
usbArray=($(cat /temp1))

echo ${usbArray[@]}

new="/temp1"
let i=0
cat ${new} | \
while read DATA1
do
	fileArray[$i]="${DATA1}"
	let j=$i+1	
	echo -e $j" "${fileArray[$i]} >>/temp2
	let i=$i+1		
done

usbArray2=($(cat /temp2))
echo ${usbArray2[@]}
sleep 0.2
Xdialog --title "USB unit list"  --menubox "Please choose " 45 80 5 ${usbArray2[@]} 
problems:
1- Xdialog --menubox does not open
2- with echo ${usbArray2[@]} all text appears in a line
ex. 1 /dev/sdc1 2 /dev/sdd1 3 /dev/sde1 ...
but, In the file it is in 2 columns. ex.
1 /dev/sdc1
2 /dev/sdd1
3 /dev/sde1
...

I think the problem is to cut the line of the ${usbArray2[@]}

help me solve the code please..
I find in google ... google ... google ... take much and still can not find how to solve

i use puppylinux slacko 6

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#4 Post by MochiMoppel »

arivas_2005 wrote:I have no programming experience
Liar! :lol:
problems:
1- Xdialog --menubox does not open
2- with echo ${usbArray2[@]} all text appears in a line
ex. 1 /dev/sdc1 2 /dev/sdd1 3 /dev/sde1 ...
If this is really the output of ${usbArray2[@]} then Xdialog should work. This code works for me:

Code: Select all

usb="1 /dev/sdc1 2 /dev/sdd1 3 /dev/sde1"
Xdialog --title "USB unit list"  --menubox "Please choose " 45 80 5 $usb
but, In the file it is in 2 columns. ex.
1 /dev/sdc1
2 /dev/sdd1
3 /dev/sde1
This is not a problem. What counts is the variable used for Xdialog, and as long as this variable is in one line, the file doesn't matter.
I think the problem is to cut the line of the ${usbArray2[@]}
I see more than one problem:
Your code (or Google's code?) is much too complicated. You do not need a file. You do not need an array. Keep it simple.

I do not understand how your code

Code: Select all

blkid | cut -d":" -f 1 | grep -v a | grep -v b 
can produce a list of usb devices. It doesn't work for me. Are you able to explain this code?

Please post the output of the blkid command and tell us what the final result, after using cut and grep, should look like? It will be much easier to help you when we have sample data.

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#5 Post by arivas_2005 »

Thanks for the prompt response

the output of the blkid command
/dev/sda4: UUID="653437F53922A242" TYPE="ntfs"
/dev/sdh1: LABEL="KINGSTON" UUID="50AF-4D72" TYPE="vfat"
/dev/sda2: LABEL="XP3" UUID="0BBEA65B43C84C62" TYPE="ntfs"
/dev/sda3: UUID="e336c409-288b-4aba-9a7e-8289b4bad009" TYPE="ext3"
/dev/sda1: LABEL="W7" UUID="2ECE073BCE06FB3B" TYPE="ntfs"
/dev/sdb1: LABEL="16G" UUID="3E6E-F263" TYPE="vfat"
/dev/sdg1: LABEL="2G" UUID="00E4404934E567A3" TYPE="ntfs"
sda (a) is hd
sdb (b) is my USB. usb attached to the pcc
other sdx. They are usb to copy (homeworks to students) or to eliminate some virus windows
at times there connected up to 6 USB

puppy linux has an excellent script mounter
however, I seek a simpler tool to open the windows of the usb and then close them as needed

again thanks for collaboration!
(P.D. and I am not "Liar!")

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#6 Post by LazY Puppy »

Don't you have any drive Icons at the desktop providing the "unmount" box?

What could be even more easy?
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#7 Post by MochiMoppel »

@arivas_2005: So you expect the output to be '/dev/sdh1 /dev/sdg1', everything except sda and sdb, right? You are probably running a full install, because in my case blkid produces

Code: Select all

/dev/loop0: TYPE="squashfs"
/dev/sda1: UUID="6B14C1E72B5727B8" TYPE="ntfs"
/dev/sda2: UUID="627469E37E706C11" TYPE="ntfs"
/dev/sda3: UUID="46F438FB5507414A" TYPE="ntfs"
/dev/sda4: LABEL="HP_TOOLS" UUID="4EF9-D3EB" TYPE="vfat"
/dev/sdb: UUID="5645-3D06" TYPE="vfat"
/dev/sdb1: UUID="5645-3D06" TYPE="vfat"
and your code would output '/dev/loop0' . I hope you can see why I was wondering. In your first post you mentioned that you "need to create a small menu to mount usb with Xdialog --menubox". My USB drive would be missing from the output.

Much room for improvement, but as a start try this:

Code: Select all

usblist=$(blkid | cut -d":" -f 1 | grep -v a | grep -v b) # create list
usblist=$(cat -n <<< "$usblist")                          # prepend each item with running number
Xdialog --title "USB unit list"  --menubox "Please choose " 45 80 5 $usblist

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#8 Post by arivas_2005 »

great!
Thanks for the solution
This is the crux of the problem

Code: Select all

usblist=$(cat -n <<< "$usblist")                          # prepend each item with running number 

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#9 Post by arivas_2005 »

sorry.
excuse my english
an exception please!

I take the opportunity of experience in using Xdialog --menubox and I discuss why the following happens:

Today I need to kill process script.
Use your code:

Code: Select all

#!/bin/bash
IFS=$'\n'
procArray=$(ps) 
procArray=$(cat -n <<< "$procArray")   # put number
Xdialog --title "process list"  --menubox "Please choose " 45 40 5 $procArray 
but leave 2 columns in the box
the selection includes only the left side
help me to build a single column
again, an exception please
thanks!
Attachments
Captura(1).png
(26.67 KiB) Downloaded 250 times

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#10 Post by MochiMoppel »

arivas_2005 wrote:but leave 2 columns in the box
?? Of course, that's the default behavior and your screenshot shows 2 columns, very far apart and probably not what you extected, but for Xdialog these are 2 columns
the selection includes only the left side
By "selection" you mean the return value? No surprise. The menubox always returns only the value of the left column
help me to build a single column again
Again? Your first example also had 2 columns. Do you really understand what your first example did and why it worked? I'm curious to know how you process the result of your first example. The Xdialog returns an arbitrary running number when you pick one of the USB devices from your menu list. How can you mount anything with only a running number?


When you fully understand your first example you will see why your process list does not work. Or, to put it positively, you will be able to make it work.

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#11 Post by arivas_2005 »

My thanks for the patience and quick response
I am a Spanish-speaking person
I'm curious to know how you process the result of your first example.
Here is code

Code: Select all

#!/bin/bash
rm -f /temp1
rm -f /temp2

blkid | cut -d":" -f 1 | grep -v a  >/temp1
mount | grep "mnt"  | cut -d " " -f3 | grep -v a | grep sd >>/temp1
nlineas=`cat /temp1 | wc -l`
let alto=$nlineas*3
awk '{print NR, $0}' /temp1 >/temp1a
mv -f -T /temp1a /temp1
usbArray=($(cat /temp1))
#echo ${usbArray[@]}
sleep 0.2
Xdialog --center --title "USB unit list"  --menubox "Please choose " $alto 30 5 "${usbArray[@]}"  2>salida
return_value=$?
#case $return_value in
 if  [ $return_value -eq 0 ]; then
		salidan=`cat salida`
			salidal=`sed -n $salidan'p' /temp1 | cut -d " " -f 2 | sed 's/^ *//g' | sed 's/ *$//' `
			echo $salidal
			if [[ "$salidal" == *"mnt"* ]] ; then
				echo "existe mnt"
				xfe -p 2 "$salidal" "/mnt/sdb1"
			else
				a="aa"
			fi
			# ver si hay subdadena en cadena
			if [[ "$salidal" == *"dev"* ]] ; then	
				partefinal=`echo ${salidal:5:10}`     
				rutamonta="/mnt/"$partefinal				
				## if [ -f /etc/hosts ];
				if [ -d $rutamonta"/" ]; then
				     mount $salidal $rutamonta
					xfe -p 2 "$rutamonta" "/mnt/sdb1"
				else
					mkdir $rutamonta"/"
					mount $salidal $rutamonta
					xfe -p 2 "$rutamonta" "/mnt/sdb1"
				fi	
			else
				a="aa"
			fi
else
	 if  [ $return_value -eq 1 ]; then
		echo "cancelar"
	 else
			 if  [ $return_value -eq 255 ]; then
				echo "boton cerrar"
			 fi
	fi	
fi
rm -f /temp1
rm -f /salida	
 
I need to do to unmount the code, but I think it will be easy with the first code

for the case ps:
In menubox there are two columns with two different processes

I can only get the first, the left one.
the process the right can not find as extract
ex.
16 371 0:00 [khugepaged] 18 372 root 0:00 [kingf..
on the same line are 16 and 18 (16 process col left. 18 process col right)

there is only access to 16 (col process left). (18 -process col right-- index impossible to access)

so I think. As it is that ps are arranged in two columns, but in case there is only one column blkid.
In both cases they are numbered 1 2 3 4 5

two possibilities
a) force a single column
b) read two columns in a row
I have no idea how to do both

Thank you again!

.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#12 Post by MochiMoppel »

arivas_2005 wrote:I am a Spanish-speaking person
I am a Japanese-speaking person
Now what? Does this make us equal or do I have to learn Spanish for you? :lol:
for the case ps:
In menubox there are two columns with two different processes
I can only get the first, the left one.
I see that you still don't understand how menubox works.

Please read the manual, or at least the help page that pops up when you make a mistake:
--menubox <text> <height> <width> <menu height> <tag1> <item1>
You can see that each line in the menu requires exactly 2 values: <tag> and <item>. Xdialog will return the value of <tag> while <item> is just a description.

Now look at your code. The running number that you add to each line will be the first value, i.e. the<tag> value. The following <item> should be one value, but when you use a simple ps command you pass 4 values: PID USER TIME COMMAND. Xdialog would take only PID as <item> and will then continue to use USER as the next <tag>, TIME as the next <item> etc. Complete chaos

Since your code contains a (useless!) IFS=$'\n', Xdialog will read the whole line (NUMBER PID USER TIME COMMAND) as 1 <tag> value and the next line as the corresponding <item> value. This is why Xdialog recognizes only line 1,3,5 etc. as <tag> values.

Make sure to pass only 2 values per menu line. Do not add a running number. Do not use IFS=$'\n'.

Here an example. The ps command will output only PID and COMMAND and Xdialog will return only PID.

Code: Select all

#!/bin/bash
pslist=$(ps -eo pid,comm --no-headings)
RESULT=$(Xdialog --title "Processes" --stdout --menubox "Please choose " 300x200 5 $pslist)
if [ "$RESULT" = "" ]; then
	Xdialog --msgbox "Nothing selected" 0 0
else
	Xdialog --msgbox "You selected PID $RESULT" 0 0
fi
Attachments
menubox.png
(11.38 KiB) Downloaded 251 times

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#13 Post by arivas_2005 »

Hi MochiMoppel

I am very grateful for your help. :D

Post Reply