You are not logged in.
@PrometheusBit python will be python3 on Arch, so no need to import division from future.
Your calculator also currently quits if the result is 0 - why not just use an infinite loop right from the start? (use while True, or something like that).
Python is fun though, enjoy relearning it.
Offline
For you, travellers, a currency converter!
#!/bin/sh
if [ $# -ne 3 ] || [ "$1" = "-h" ]; then
cat <<EOF
Usage: ${0##*/} VALUE IN-CURRENCY OUT-CURRENCY
Convert VALUE from IN-CURRENCY to OUT-CURRENCY.
CURRENCY is a 3-letters code like EUR, SEK, USD, etc.
EOF
exit
fi
curl -sA "Mozilla/5.0" "http://www.google.com/finance?q=$2$3" | \
awk -v value="$1" -F '<|>' '/^1 / {print value * substr($3, 1, index($3," "))}'
Offline
Really handy: thank you!
Offline
Really handy: thank you!
Agreed. I wish this forum had a +1 feature. It always annoyed me whenever I checked currency how long it took because most sites you have to select from drop downs and then enter the value and then wait for it to generate. This works surprisingly fast.
EDIT: In case anyone wants to browse the 3 character codes used/extend the script you can find google's list here
Last edited by basica (2015-10-08 08:43:32)
Offline
Thanks for the feedback!
@basica: thanks for the link, but you should remove the 'view-source' as it does not work on non-chrome-based browsers.
Offline
I thought it would be nice to include the currency code list in the program. So be it!
#!/bin/sh
[ -z "$AGENT" ] && AGENT='curl -sA "Mozilla/5.0"'
if [ "$1" = "-l" ]; then
$AGENT 'https://www.google.com/finance/converter' \
| awk -F'<|>|"' '/select name=from value/, /\/select/ {if ($5 != "") {sub(/\(.*\)/, "", $5); print $3, $5}}'
exit
fi
usage() {
cat <<EOF
Usage: ${0##*/} IN-CURRENCY OUT-CURRENCY [VALUES...]
${0##*/} -l
Convert VALUES from IN-CURRENCY to OUT-CURRENCY.
VALUES are read from stdin if not provided.
CURRENCY is a 3-letters code like EUR, SEK, USD, etc.
See the list of codes with the '-l' option.
The download agent is specified from environment variable AGENT='$AGENT'.
EOF
}
if [ $# -lt 2 ]; then
usage
exit
fi
change="$($AGENT "http://www.google.com/finance?q=$1$2" | \
awk -F '<|>' '/^1 / {print substr($3, 1, index($3, " "))}')"
if [ -z "$change" ]; then
usage
exit
fi
shift 2
mul() {
[ -n "$1" ] && [ -n "$2" ] && awk "BEGIN {print $1*$2}"
}
if [ -z "$1" ]; then
while read v; do
mul $v $change
done
else
for v; do
mul $v $change
done
fi
Other changes include:
* VALUES can be multiple. They are the last arguments and they are read from stdin if not provided (Unix-style).
* Download agent is taken from the envionment variable AGENT. Maybe a little overkill... ?
Last edited by Ambrevar (2015-10-08 08:51:44)
Offline
I couldn't find a way to include AUR updates in Conky, so I did this:
cat /usr/share/applications/pacup.desktop
[Desktop Entry]
Encoding=UTF-8
Version=0.0.0-1
Type=Application
Name=pacup
Comment=
Exec=/home/betseg/Documents/pacupdatecheck2.sh
StartupNotify=false
Terminal=false
Hidden=false
Icon=
/home/betseg/Documents/pacupdatecheck2.sh
while [ 1 ];
do
echo $(checkupdates | wc -l) + $(pacaur -k | wc -l) | bc > /home/betseg/updatecount;
sleep 30m;
done
.conkyrc
[...]
${exec cat /home/betseg/updatecount}
[...]
This doesn't work:
${execpi 1800 echo $(checkupdates | wc -l) + $(pacaur -k | wc -l) | bc}
Anyone knows a better way?
Last edited by betseg (2015-10-08 09:14:53)
Offline
Came up with this for Unity in Ubuntu Kylin, but possibly useful for other gnome-derivative desktops using ibus.
Switching ibus IMEs stopped working outside of GNOME-Shell.
I suspect it's become dependent on a dbus service provided by the shell, so fails in other desktops (this kind of thing has been happening more and more with GNOME components).
So made my own means of switching IMEs, and set this to a hotkey:
#!/bin/bash
if [ "$1" == "next" ]; then
case "$(ibus engine)" in \
"xkb:jp::jpn") ibus engine "pinyin"; ;; \
"pinyin") ibus engine "sunpinyin" ;; \
"sunpinyin") ibus engine "anthy" ;; \
"anthy") ibus engine "xkb:jp::jpn" ;; esac
elif [ "$1" == "previous" ]; then
case "$(ibus engine)" in \
"xkb:jp::jpn") ibus engine "anthy"; ;; \
"pinyin") ibus engine "xkb:jp::jpn" ;; \
"sunpinyin") ibus engine "pinyin" ;; \
"anthy") ibus engine "sunpinyin" ;; esac
fi
makepkg-optimize · indicator-powersave · pantheon-{3d,lite} · {pantheon,higan}-qq
Offline
Good lord, that is uglified. You don't need any of those escapes and you seem to have sprinkled some redundant ;'s in there as well...
Offline
Same result but easily extensible by adding or reordering your engines:
#!/bin/bash
engines=( 'xkb:jp::jpn' 'pinyin' 'sunpinyin' 'anthy' )
n=$((${#engines[@]} - 1))
for i in $(seq 0 $n); do
[[ "$(ibus engine)" == "${engines[$i]}" ]] && break
done
[[ $i -eq n ]] && i=-1
case "$1" in
nex*) echo ibus engine ${engines[(($i + 1))]} ;;
pre*) echo ibus engine ${engines[(($i - 1))]} ;;
*) echo "usage: $0 [next | prev]"
esac
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Pedantic, but a C-style for loop spawns one less process:
for (( i=0; i<=$n; i++ )); do [...]
Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby
Offline
array=(1 2 3)
for elem in "${array[@]}"; do
...
done
There are plenty of other poor practices but this one is a strange mistake.
For example, an array subscript is an arithmatic context, so using $(( ... )) doesn't make any sense, it makes even less sense when $(( ... )) could hve been used but test's -eq was chosen instead.
Other more nitpicky things involve the quoting of $1 for the case expression which is unnecessary, the use of && and || instead of a proper if then else fi construct and using - 1 on the array length.
Last edited by Earnestly (2015-10-14 16:23:22)
Offline
Feel free to show a working example without those issues. But I'm not sure whose strange mistake you are pointing out as that code isn't in any of the recent posts.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
@Trilby: I think the last 2 posts referred to your code
for i in $(seq 0 $n); do
What you wrote is POSIX. It will spawn one process for `seq`. But since you wrote it in Bash, you are better off using the native C-style loop as mentioned by Alad.
Offline
I just hacked this together now, maybe there's a better way of doing this but essentially I sometimes search for a package and if I want to install it, to be able to do it with as little typing as possible. Any suggestions for the script welcome. I know I need to add some error handling, will get around to that soon and update the script here.
#!/bin/bash
search_term="$1"
if [[ -z "$search_term" ]]; then
echo "please enter a search term"
exit
fi
pacman -Ss "$search_term" | awk 'NR % 2 { printf "%s) %s\n", i += 1, $0 } !(NR % 2) { print $0; }'
read -p "which package do you wish to install (q to cancel): " usrans
if [[ "$usrans" == "q" ]]; then exit; fi
sudo pacman -S $(pacman -Ssq "$search_term" | sed -n "$usrans"p)
EDIT: Updated code with thanks to Trilby.
Last edited by basica (2015-10-19 12:16:37)
Offline
No need for the tempfile, just loop through the pacman output - and you could do that entire loop with an awk one-liner:
pacman -Ss "$search_term" | awk 'NR % 2 { printf "%s) %s\n", i += 1, $0 } !(NR % 2) { print $0; }'
read -p ...
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
No need for the tempfile, just loop through the pacman output - and you could do that entire loop with an awk one-liner:
pacman -Ss "$search_term" | awk 'NR % 2 { printf "%s) %s\n", i += 1, $0 } !(NR % 2) { print $0; }' read -p ...
That's pretty awesome, thanks. I keep telling myself I need to familiarize myself with awk, and this is another reason why.
EDIT: I spent some time trying to parse this code (because it seemed like black magic ..) and I'm now even more impressed now that I understand what's going on. For anyone who is as clueless as I was about it, let me break it down a bit. The NR % 2 basically checks the line number, even lines are prepended with the value of the i variable) and uneven are printed as is. This section "{ printf "%s) %s\n", i += 1, $0 }" was what really threw me off in the beginning, but that's because the incrementing of the variable i is done in place. Quite impressive indeed!
Last edited by basica (2015-10-19 12:38:57)
Offline
Consider using bash's select. For example:
#!/bin/bash
# Select's prompt. Instead of using 'q' or some other marker just reuse the
# shells ability to react to EOF (^D) instead.
PS3="which package do you wish to install (^D to continue): "
if ! (($#)); then
printf 'Usage: %s <search terms>\n' "${0##*/}"
exit 1
fi
# Gather the results in an array.
packages=($(pacman -Ssq "$@"))
select pick in "${packages[@]}"; do
if [[ $pick ]]; then
picks+=("$pick")
printf 'Selected: %s\n' "${picks[*]}"
fi
done
if [[ $picks ]]; then
printf 'Installing %s\n' "${picks[*]}"
sudo pacman -S "${picks[@]}"
fi
The beauty of this is that select will continue until EOF, exploiting this can be used to build up an array of packages and passing them all at once to pacman instead of in succession.
I don't see much value in trying to remove entries because pacman will prompt for continuation itself and I believe that providing your own diagnostics and prompts instead of just falling on the tools own output is redundant.
Also if you need a more custom output than what -Ssq provides, consider using expac instead to better tailor it to your needs instead of using sed or awk for filtering. (It's better to avoid filtering if unnecessary.)
For example:
declare -A packages
while read -r head tail; do
packages["$head"]="$tail"
done < <(expac -Ss '%n %d' "$@")
Edit: As for awk, it's a bit too much of an assumption I would not make, however you can do the same thing with a while read loop using command grouping without the need to invoke awk:
while {
read -r line1
read -r line2
} do
stuff_with "$line1" "$line2"
... blah
}
In this example while will read two lines for each iteration and assign the respective lines into the variables defined by read. As you can imagine this could expand to more if necessary. You essentially take a block of reads and use a while loop to move that block over the file you're reading, capturing the lines as you go, thanks to Linux's file IO API \o/
Edit2: A good resource for awk related material: http://awk.freeshell.org/
Last edited by Earnestly (2015-10-19 15:16:34)
Offline
Earnestly, thanks for your suggestions as well. The main reason I have it set up the way I do is so I can search for something, and see if it's the thing I am after based on either its name or description and then enter the number for that particular package to install it. I'd rather not install another package for this functionality, as it is a rather simple script so I don't see too much harm in filtering on this occasion, but once again thanks for the suggestions.
Offline
For similar purpose I use this (as a part of bigger pacman menu script):
sudo pacman -S $(pacman -Slq | pmenu -p 'select package to install >')
Requires pmenu. It is really fast for browsing available packages, but Earnestlys suggestion is superior in allowing selection of multiple items. Your script is nice too basica .
The difference between reality and fiction is that fiction has to make sense.
Offline
This is probably my most hackish yet but it was an excuse to give me something to hack on. What this does is run archey3 which itself takes a screenshot and then uploads the file to imgur and puts into your clipboard (requires xclip) the bbcode you need to have a thumbnail pic of your screenshot linked to the fullsize version. An output example taken from my latest post in the screenshot thread is the following:
[\url=http://i.imgur.com/CLbrug8.jpg][\img]http://i.imgur.com/CLbrug8m.jpg[\img][\url]
Oh, it also prints to the screen a deletion page link too. Once again, suggestions/improvements welcome and I'm sure there's a few since you could probably do what I do in sed, with awk just for starters. Anyways, here's the code:
client_id="insert clientid here" # https://api.imgur.com/oauth2/addclient
archey3 -s
screenshot="$(ls | egrep [0-9]{2}_[0-9]{2}:[0-9]{2}:[0-9]{2}_20[1-9][0-9].jpg)"
response=$(curl -silent -H "Authorization: Client-ID $client_id" -F "image=@${screenshot}" https://api.imgur.com/3/image.json)
url=$(echo $response | awk '{split($0,a,","); print a[41]}' | sed 's/\(\\\|"link":\|}\|"\)//g')
mini="${url%.jpg}m.jpg"
forum_code="[url=${url}][img]${mini}[/img][/url]"
deletehash=$(echo $response | awk '{split($0,a,","); print a[39]}' | sed 's/\("deletehash":\|"\)//g')
echo "deletion page: http://imgur.com/api/delete/${deletehash}.json"
echo "$forum_code" | xclip -selection c
rm -v "$screenshot"
Offline
Update download size finder thing.
upd=$(checkupdates); if [ -z "$upd" ]; then echo "No updates" ; else echo "Download size:" ; echo $( echo $(expac -S -H M '%k\t%n' $upd | awk '{print $1; print "+";}') | sed s'/.$//') | bc ; echo "MB"; fi
upd=$(checkupdates)
if [ -z "$upd" ]
then
echo "No updates"
else
echo "Download size:"
echo $( echo $( expac -S -H M '%k\t%n' $upd | awk '{print $1; print "+";}' ) | sed s'/.$//' ) | bc
echo "MB"
fi
Example:
$ checkupdates
breeze-icons
plasma-workspace-wallpapers
# pacman -S breeze-icons plasma-workspace-wallpapers
resolving dependencies...
looking for conflicting packages...
Packages (2) breeze-icons-5.4.2-2 plasma-workspace-wallpapers-5.4.2-1
Total Download Size: 55.79 MiB
Total Installed Size: 88.34 MiB
:: Proceed with installation? [Y/n]
$ expac -S -H M '%k\t%n' breeze-icons plasma-workspace-wallpapers
1.93 MiB breeze-icons
53.87 MiB plasma-workspace-wallpapers
$ [the script]
Download size:
55.80
MB
Last edited by betseg (2015-10-31 20:01:31)
Offline
Note the following two do the same thing:
echo $( echo $( expac -S -H M '%k\t%n' $upd | awk '{print $1; print "+";}' ) | sed s'/.$//' ) | bc
expac -SH M '%k' $upd | awk '{sum+=$1;} END { print sum; }'
But the second one saves two echos, a sed, and a bc.
But even better you can get rid of the other echos too:
expac -SH M '%k' $upd | awk '{ sum+=$1; } END { printf "Download Size: %s\n", sum; }'
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Wow, that's really better. I'm learning new things everyday. Thanks!
Last edited by betseg (2015-11-01 09:08:14)
Offline
@ Basica (or all "iso 2 usb" interested ones)
Just finished the update i was working on for the last 2 days, only 'dd' output specific, as the progress display was totaly borked....
Me have a handy tool, since about 2 years, called TUI (as in Text User Interface), it comes (among lots of other stuff) with a wrapper around dd, which one would call like:
sudo tui-dd archlinux-2015.12.01-dual.iso
Which will look like:
# | Use USB as target? (y/n) y | #
# | 1) usb-Multiple_Card_Reader_058F63666485-0:0 2) usb-TOSHIBA_External_USB_3.0_20150915003355F-0:0 | #
# | > 1
# | Starting job, please wait... [ WORK ] | #
# | archlinux-2015.12.01-dual.iso (0 MB) to /dev/sdb [ / ] | #
# | Writing archlinux-2015.12.01-dual.iso to /dev/sdb (572 MB/663 MB) [================== ][ 86.27% ] | #
# | Synchronizing /dev/sdb [ \ ] | #
# | Successfully written archlinux-2015.12.01-dual.iso to /dev/sdb. [ DONE ] | #
Note that from "Starting job.." until "Successfully" the real output is on a single line, displaying the 'current task', just pressed enter for 'illustration'.
If i'd be root already, and not calling sudo, i'd also would have "∞" for "WORK", "✗" and "✓" for FAIL or DONE respectivly. (colored anyway: yellow, red, green )
Depending on your system, sync may take longer than 'writing the iso', or may not be required (show up) at all.
Code :: http://git.savannah.gnu.org/cgit/tui.gi … bin/tui-dd (0.9.2 is fine, but does not contain the last status line (DONE), few more little adjustments, and tui-dd 0.9.3 should be done)
Project: https://savannah.nongnu.org/projects/tui/ (0.9.2 - same version as tui-dd is coincident)
PS:
Likewise tui-download (handles curl/wget), but that looks alot less impressive in comparision
Have fun
Last edited by esa (2015-12-03 23:43:41)
Offline