LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 06-11-2020, 10:58 AM   #16
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Original Poster
Rep: Reputation: 0

Quote:
Originally Posted by shruggy View Post
Put a space bethween " and ]]. I'd probably just test for presence of the string:
Code:
if dmesg -S|grep -q "USB Mass Storage device detected"; then
yes i wanted to this check for the presence of the string too, but didn't know how to do it. so you check for a string -q flag?
 
Old 06-11-2020, 11:10 AM   #17
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
No worries...as long as you're learning, you'll keep getting better. You're missing a space after the detected" and before the ]]. That should get rid of that message. And after thinking about it for a bit, try this instead:
Code:
if [[ "$(dmesg -S | tail | grep "usb-storage")" =~ "USB Mass Storage device detected" ]]; then
           icon=" ��️ "
           else
           icon=""
   fi
   
   printf "%s%s\\n" "$icon"
...which will check for the presence of the string in what you get. That way, you can modify this to do different things if you plug in a mouse or some other USB device.
yes this is working. but the script that i sent after adding the space it didn't give any error but also wont show anything when usb connected. so i copied yours. can you tell me what the ~ after = means.
 
Old 06-11-2020, 11:13 AM   #18
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
From the Bash Reference Manual:
Quote:
An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered a POSIX extended regular expression and matched accordingly (as in regex(3)).

Last edited by shruggy; 06-11-2020 at 11:18 AM.
 
Old 06-11-2020, 11:18 AM   #19
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
So that's the device you need to query with fuser.../dev/snd/pcmC1D0c, and incorporate that into your script.
ok so for microphone i did this
Code:
   #!/bin/bash
   
        if [[ "$(fuser /dev/snd/pcmC1D0c)" != "" ]]; then
          icon="  "
        else
          icon=""
        fi
   
   printf "%s%s\\n" "$icon"
its working it doesn't show anything when mic not is use but when in use it shows icon but its also giving me this output when i run the script in terminal
Code:
/dev/snd/pcmC1D0c:  m
 
 
Old 06-11-2020, 11:24 AM   #20
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by shruggy View Post
thank you i bookmarked the page for future reference. i guess i can `man bash` also.?
 
Old 06-11-2020, 11:27 AM   #21
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
fuser prints the name of device to stderr. So, add 2>/dev/null
Code:
if [[ -n "$(fuser /dev/snd/pmcC1D0c 2>/dev/null)" ]]; then
Quote:
Originally Posted by apoorv569 View Post
i guess i can `man bash` also.?
Yes, it contains the same text.

Last edited by shruggy; 06-11-2020 at 11:29 AM.
 
Old 06-11-2020, 11:39 AM   #22
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by shruggy View Post
fuser prints the name of device to stderr. So, add 2>/dev/null
Code:
if [[ -n "$(fuser /dev/snd/pmcC1D0c 2>/dev/null)" ]]; then

Yes, it contains the same text.
what is the -n for that you added in the beginning. and what does 2> mean? i guess /dev/null is like show nothing?
if you can point me to a man page or reference guide for this .
 
Old 06-11-2020, 12:06 PM   #23
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
No worries...as long as you're learning, you'll keep getting better. You're missing a space after the detected" and before the ]]. That should get rid of that message. And after thinking about it for a bit, try this instead:
Code:
if [[ "$(dmesg -S | tail | grep "usb-storage")" =~ "USB Mass Storage device detected" ]]; then
           icon=" ��️ "
           else
           icon=""
   fi
   
   printf "%s%s\\n" "$icon"
...which will check for the presence of the string in what you get. That way, you can modify this to do different things if you plug in a mouse or some other USB device.
all scripts are working now. but this usb script, when i plug a usb drive it shows icon as expected, but if plug another usb in different port,at the same time so i have now 2 usb's, the icon disappears.
 
Old 06-11-2020, 12:12 PM   #24
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Quote:
Originally Posted by apoorv569 View Post
what is the -n for that you added in the beginning.
See Bash Conditional Expressions. If you omit -n it has the same effect: true if string is not empty.
Quote:
Originally Posted by apoorv569 View Post
and what does 2> mean? i guess /dev/null is like show nothing?
See this answer as well as File Redirection in BashGuide and I/O Redirection in Advanced Bash-Scripting Guide.

2 is the file descriptor (FD) for standard error stream (stderr).

Last edited by shruggy; 06-11-2020 at 12:21 PM.
 
Old 06-11-2020, 12:21 PM   #25
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by shruggy View Post
See Bash Conditional Expressions. If you omit -n it has the same effect: true if string is not empty.

See File Redirection in BashGuide as well as I/O Redirection in Advanced Bash-Scripting Guide.

2 is the file descriptor (FD) for standard error stream (stderr).
thank you.i have bookmarked all the pages.
 
Old 06-11-2020, 11:33 PM   #26
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
No worries...as long as you're learning, you'll keep getting better. You're missing a space after the detected" and before the ]]. That should get rid of that message. And after thinking about it for a bit, try this instead:
Code:
if [[ "$(dmesg -S | tail | grep "usb-storage")" =~ "USB Mass Storage device detected" ]]; then
           icon=" ��️ "
           else
           icon=""
   fi
   
   printf "%s%s\\n" "$icon"
...which will check for the presence of the string in what you get. That way, you can modify this to do different things if you plug in a mouse or some other USB device.
this script is not good enough. when i connect 1 usb it gives this
Code:
[  151.134547] usb-storage 1-2:1.0: USB Mass Storage device detected
[  151.138129] scsi host2: usb-storage 1-2:1.0
[  152.157810] scsi 2:0:0:0: Direct-Access     MBIL SSM Moser Baer Disk  8.07 PQ: 0 ANSI: 4
[  152.158051] sd 2:0:0:0: Attached scsi generic sg2 type 0
[  152.158711] sd 2:0:0:0: [sdb] 31027200 512-byte logical blocks: (15.9 GB/14.8 GiB)
[  152.159381] sd 2:0:0:0: [sdb] Write Protect is off
[  152.159387] sd 2:0:0:0: [sdb] Mode Sense: 23 00 00 00
[  152.160066] sd 2:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[  152.924036]  sdb: sdb1 sdb2 sdb3
[  152.926370] sd 2:0:0:0: [sdb] Attached SCSI removable disk
when i connect another usb at the same time it gives this
Code:
[  364.948449] scsi host3: usb-storage 1-1:1.0
[  366.206529] scsi 3:0:0:0: Direct-Access     SRT      USB              1100 PQ: 0 ANSI: 4
[  366.206834] sd 3:0:0:0: Attached scsi generic sg3 type 0
[  366.207126] sd 3:0:0:0: [sdc] 15818752 512-byte logical blocks: (8.10 GB/7.54 GiB)
[  366.207629] sd 3:0:0:0: [sdc] Write Protect is off
[  366.207631] sd 3:0:0:0: [sdc] Mode Sense: 43 00 00 00
[  366.208117] sd 3:0:0:0: [sdc] No Caching mode page found
[  366.208119] sd 3:0:0:0: [sdc] Assuming drive cache: write through
[  366.237610]  sdc: sdc1
[  366.239332] sd 3:0:0:0: [sdc] Attached SCSI removable disk
which doesn't have usb-storage to grep so the icon disappears even though i have 2 usb connected.

when i connect a usb portable 2.5" hdd it gives this ( i extended tail range to -n 15 )
Code:
[  659.426376] usb 1-3: Product: USB 3.0 Device
[  659.426377] usb 1-3: Manufacturer: USB 3.0 Device
[  659.426379] usb 1-3: SerialNumber: 00000000B032
[  659.433060] scsi host4: uas
[  659.433839] scsi 4:0:0:0: Direct-Access     WDC WD10 SPZX-00Z10T0     0117 PQ: 0 ANSI: 6
[  659.435213] sd 4:0:0:0: Attached scsi generic sg4 type 0
[  659.435840] sd 4:0:0:0: [sdd] 1953525168 512-byte logical blocks: (1.00 TB/932 GiB)
[  659.435842] sd 4:0:0:0: [sdd] 4096-byte physical blocks
[  659.436038] sd 4:0:0:0: [sdd] Write Protect is off
[  659.436041] sd 4:0:0:0: [sdd] Mode Sense: 53 00 10 08
[  659.436349] sd 4:0:0:0: [sdd] Disabling FUA
[  659.436351] sd 4:0:0:0: [sdd] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[  659.436476] sd 4:0:0:0: [sdd] Optimal transfer size 33553920 bytes not a multiple of physical block size (4096 bytes)
[  659.578561]  sdd: sdd1
[  659.580012] sd 4:0:0:0: [sdd] Attached SCSI disk
usb hdd doesn't even have a usb-storage to grep.

also when 1 of the flash drive was connected and i rebooted the icon didn't show because the dmesg tail was about my wifi being connected and networkmanager.

any idea's for improving this script, as this is not reliable.
 
Old 06-12-2020, 04:29 AM   #27
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Original Poster
Rep: Reputation: 0
i serched the internet a little and found that i could use udev for detecting usb. but im not sure how to use use udev to get the info i need. but i did find this command
Code:
udevadm monitor --kernel --property --subsystem-match=usb
so i tried making the script like this
Code:
   #!/bin/bash
  
   if [[ "$(udevadm monitor --kernel --property --subsystem-match=usb | head -n 4 | tail -n 1 | awk '{print $2}')" == "add" ]]; then
           icon="  "
   #elif [[ "$(udevadm monitor --kernel --property --subsystem-match=usb | head -n 4 | tail -n 1 | awk '{print $2}')" =~ "unbind" ]]; then
           icon=""
   fi
  
   printf "%s%s\\n" "$icon"
however when i run the script it waits for me to plug usb to show, and when i added it in my block list dwmblock did not show at all i had to remove it. i dont know how to use udev to extract the data.
 
Old 06-12-2020, 06:17 AM   #28
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Well, udevadm monitor will just sit there and wait for events until you kill it. It will never transfer control to your script, so the script will be stuck reading the output from udevadm monitor forever.

Besides, you're only interested in storage devices, right? So you don't want the script to fire each time you plug in a new USB mouse or some such.

I'd rather evaluate the output of udisksctl dump like I did in that thread. This should be enough:
Code:
udisksctl dump|grep 'HintAuto:\s*true'

Last edited by shruggy; 06-12-2020 at 06:40 AM.
 
Old 06-12-2020, 06:51 AM   #29
apoorv569
Member
 
Registered: Jun 2020
Distribution: Arch Linux
Posts: 61

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by shruggy View Post
Well, udevadm monitor will just sit there and wait for events until you kill it. It will never transfer control to your script, so the script will be stuck reading the output from udevadm monitor forever.

Besides, you're only interested in storage devices, right? So you don't want the script to fire each time you plug in a new USB mouse or some such.

I'd rather evaluate the output of udisksctl dump like I did in that thread. This should be enough:
Code:
udisksctl dump|grep 'HintAuto:\s*true'
for now i just want it for storage device.

this command
Code:
udisksctl dump|grep 'HintAuto:\s*true'
gives me
Code:
    HintAuto:                   true
even when there is no usb is connected.

but the link you sent me for ubuntu forums i think i can use this
Code:
udisksctl dump|awk -F':\n' -v'RS=\n\n' '/[ \t]*HintAuto:[ \t]*true/&&/\.Filesystem:/{sub(/.*\/UDisks2\//,"",$1); print $1}'
as this doesn't gives any output when no usb is connected, and shows (using all 3 usb ports)
Code:
block_devices/sdb1
block_devices/sdc1
block_devices/sdd1
block_devices/sdd2
block_devices/sdd3
maybe something like this
Code:
if [[ "$(udisksctl dump|awk -F':\n' -v'RS=\n\n' '/[ \t]*HintAuto:[ \t]*true/&&/\.Filesystem:/{sub(/.*\/UDisks2\//,"",$1); print $1}')" != " " ]]; then
           icon="  "
   else
           icon=""
   fi
  
   printf "%s%s\\n" "$icon"
 
Old 06-12-2020, 07:42 AM   #30
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Ah, I don't have a DVD drive or card reader, so didn't think about other kinds of devices beyond USB that may have HintAuto set to true.

Well, the awk script enumerates partitions on all removable storage devices. I believe in your case it could be a bit simplified. How about
Code:
udisksctl dump|awk -F':\n' -vRS= '/[ \t]*HintAuto:[ \t]*true/&&/\.Filesystem:/{print "found"}'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: dwm: A Minimalist Tiling Window Manager For Linux LXer Syndicated Linux News 0 04-03-2018 02:52 PM
LXer: Top 4 reasons I use dwm for my Linux window manager LXer Syndicated Linux News 0 07-19-2017 04:40 AM
dwm window manager: conky eats 100%CPU when it has its own window (window_type normal) ondoho Linux - Software 0 10-11-2016 08:49 PM
Get window size with tiling window manager in SDL Snark1994 Programming 1 02-24-2013 08:46 PM
Config-file for DWM window manager on Debian. tirka Linux - Newbie 1 06-25-2011 06:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 04:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration