LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-04-2013, 08:22 AM   #1
stozi
Member
 
Registered: Mar 2010
Distribution: Void Linux
Posts: 39

Rep: Reputation: 0
Is my bash file stupid, can it be simplified?


I made a couple pipe menus for Openbox over the last couple of days, which was fun as I knew nothing about bash previously. One displayed the output of df but it seems a limit to menu width undermined this. This one, on the other hand, works like a dandy for me. I don't understand entirely how it works, so for the sake of my own bash/awk education, I ask you, can it be simplified without using bc or acpi or something else I might not have installed?

Code:
#!/bin/sh

now=`cat /sys/class/power_supply/BAT0/charge_now`
full=`cat /sys/class/power_supply/BAT0/charge_full`

Date=$(date '+%R %a %x')
Wifi=$(cat /proc/net/wireless | awk 'NR==3 {print $3}')
Batt=$(echo $now $full | awk '{ printf("%.2f\n", $1/$2 * 100) }')

echo "<openbox_pipe_menu>"

echo "<separator label=\"$Date\"/>"
echo "<separator />"
echo "<item label=\"Wifi:  $Wifi%\"/>"
echo "<item label=\"Batt: $Batt%\"/>"

echo "</openbox_pipe_menu>"
 
Old 09-04-2013, 09:27 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,481

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
All looks pretty sensible to me. Might just want to change the first line to:

Code:
#!/bin/bash
To ensure that bash shell is used rather than whatever /bin/sh is linked to.
 
Old 09-04-2013, 09:33 AM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I also recommend using $() instead of back ticks ``, as they are deprecated and will be removed at some point in the future.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
stupid bash question, how to CD and stay in the new dir? MrUmunhum Linux - General 3 05-27-2009 09:04 PM
Need help with bash script /simplified sort command/ szcs Linux - General 0 10-26-2008 11:40 AM
stupid bash question tisleti Linux - Newbie 4 09-10-2004 05:34 AM
Stupid bash problem ericcarlson Linux - Newbie 3 07-17-2004 02:19 AM
bash script - even more stupid question than last time. webamoeba Linux - Newbie 2 02-12-2004 03:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:31 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