How to add weather to i3status in NixOS - weather

Adding weather to the status bar in i3 can be done in several ways, including:
py3status
piping i3status to a custom bash script
i3status does not allow including arbitrary shell commands in the configuration file. NixOS environment for Python requires further configuration, and when I pipe i3status I lose the color formatting. How do I preserve color formatting and add weather without adding additional i3 extensions?

Add a shell script to /etc/nixos/i3/weather.sh (modified from Reddit user #olemartinorg):
#!/bin/sh
# weather.sh
# shell script to prepend i3status with weather
i3status -c /etc/nixos/i3/i3status.conf | while :
do
read line
weather=$(cat ~/.weather.cache)
weather_json='"name":"weather","color":"#FFFFFF", "full_text":'
weather_json+=$(echo -n "$weather" | python -c 'import json,sys; print json.dumps(sys.stdin.read())')
weather_json+='},{'
# Inject our JSON into $line after the first [{
line=${line/[{/[{$weather_json}
echo "$line" || exit 1
done
Create a cronjob in your NixOs configuration.nix:
services.cron = {
enable = true;
systemCronJobs = [
"*/5 * * * * USERNAME . /etc/profile; curl -s wttr.in/Osnabrueck?format=3 > ~/.weather.cache"
];
};
Replace "Osnabrueck" with your city name, and USERNAME with your username. This creates a file .weather.cache which will contain the local weather as a one-liner.
Finally, update i3.conf, replacing i3status with the path to your script:
bar {
status_command /etc/nixos/i3/weather.sh
tray_output primary
}
nixos-rebuild switch and start i3 ($mod+Shift+R). You should now see your weather at the bottom (or wherever your i3 status bar displays).

Related

assign two bash arrays with a single command

youtube-dl can take some time parsing remote sites when called multiple times.
EDIT0 : I want to fetch multiple properties (here fileNames and remoteFileSizes) output by youtube-dl without having to run it multiple times.
I use those 2 properties to compare the local file size and ${remoteFileSizes[$i]} to tell if the file is finished downloading.
$ youtube-dl --restrict-filenames -o "%(title)s__%(format_id)s__%(id)s.%(ext)s" -f m4a,18,webm,251 -s -j https://www.youtube.com/watch?v=UnZbjvyzteo 2>errors_youtube-dl.log | jq -r ._filename,.filesize | paste - - > input_data.txt
$ cat input_data.txt
Alan_Jackson_-_I_Want_To_Stroll_Over_Heaven_With_You_Live__18__UnZbjvyzteo__youtube_com.mp4 8419513
Alan_Jackson_-_I_Want_To_Stroll_Over_Heaven_With_You_Live__250__UnZbjvyzteo__youtube_com.webm 1528955
Alan_Jackson_-_I_Want_To_Stroll_Over_Heaven_With_You_Live__140__UnZbjvyzteo__youtube_com.m4a 2797366
Alan_Jackson_-_I_Want_To_Stroll_Over_Heaven_With_You_Live__244__UnZbjvyzteo__youtube_com.webm 8171725
I want the first column in the fileNames array and the second column in the remoteFileSizes.
For the time being, I use a while read loop, but when this loop is finished my two arrays are lost :
$ fileNames=()
$ remoteFileSizes=()
$ cat input_data.txt | while read fileName remoteFileSize; do \
fileNames+=($fileName); \
remoteFileSizes+=($remoteFileSize); \
done
$ for fileNames in "${fileNames[#]}"; do \
echo PROCESSING....; \
done
$ echo "=> fileNames[0] = ${fileNames[0]}"
=> fileNames[0] =
$ echo "=> remoteFileSizes[0] = ${remoteFileSizes[0]}"
=> remoteFileSizes[0] =
$
Is it possible to assign two bash arrays with a single command ?
You assign variables in a subshell, so they are not visible in the parent shell. Read https://mywiki.wooledge.org/BashFAQ/024 . Remove the cat and do a redirection to solve your problem.
while IFS=$'\t' read -r fileName remoteFileSize; do
fileNames+=("$fileName")
remoteFileSizes+=("$remoteFileSize")
done < input_data.txt
You might also interest yourself in https://mywiki.wooledge.org/BashFAQ/001.
For what it's worth, if you're looking for specific/bespoke functionality from youtube-dl, I recommend creating your own python scripts using the 'embedded' approach: https://github.com/ytdl-org/youtube-dl/blob/master/README.md#embedding-youtube-dl
You can set your own signal for when a download is finished (text/chime/mail/whatever) and track downloads without having to compare file sizes.

change folder modified date based on most recent file modified date in folder

I have a number of project folders that all got their date modified set to the current date & time somehow, despite not having touched anything in the folders. I'm looking for a way to use either a batch applet or some other utility that will allow me to drop a folder/folders on it and have their date modified set to the date modified of the most recently modified file in the folder. Can anyone please tell me how I can do this?
In case it matters, I'm on OS X Mavericks 10.9.5. Thanks!
If you start a Terminal, and use stat you can get the modification times of all the files and their corresponding names, separated by a colon as follows:
stat -f "%m:%N" *
Sample Output
1476985161:1.png
1476985168:2.png
1476985178:3.png
1476985188:4.png
...
1476728459:Alpha.png
1476728459:AlphaEdges.png
You can now sort that and take the first line, and remove the timestamp so you have the name of the newest file:
stat -f "%m:%N" *png | sort -rn | head -1 | cut -f2 -d:
Sample Output
result.png
Now, you can put that in a variable, and use touch to set the modification times of all the other files to match its modification time:
newest=$(stat -f "%m:%N" *png | sort -rn | head -1 | cut -f2 -d:)
touch -r "$newest" *
So, if you wanted to be able to do that for any given directory name, you could make a little script in your HOME directory called setMod like this:
#!/bin/bash
# Check that exactly one parameter has been specified - the directory
if [ $# -eq 1 ]; then
# Go to that directory or give up and die
cd "$1" || exit 1
# Get name of newest file
newest=$(stat -f "%m:%N" * | sort -rn | head -1 | cut -f2 -d:)
# Set modification times of all other files to match
touch -r "$newest" *
fi
Then make that executable, just necessary one time, with:
chmod +x $HOME/setMod
Now, you can set the modification times of all files in /tmp/freddyFrog like this:
$HOME/setMod /tmp/freddyFrog
Or, if you prefer, you can call that from Applescript with a:
do shell script "$HOME/setMod " & nameOfDirectory
The nameOfDirectory will need to look Unix-y (like /Users/mark/tmp) rather than Apple-y (like Macintosh HD:Users:mark:tmp).

importing data from a CSV in Bash

I have a CSV file that I need to use in a bash script. The CSV is formatted like so.
server1,file.name
server1,otherfile.name
server2,file.name
server3,file.name
I need to be able to pull this information into either an array or in some other way so that I can then filter the information and only pull out data for a single server that i can then pass to another command within the script.
I need it to go something like this.
Import workfile.csv
check hostname | return only lines from workfile.csv that have the hostname as column one and store column 2 as a variable.
find / -xdev -type f -perm -002 | compare to stored info | chmod o-w all files not in listing
I'm stuck using bash because of the environment that I'm working in.
The csv can be to big for adding all filenames in the find parameter list.
You also do not want to call find in a loop for every line in the csv.
Solution:
First make a complete list of files in a tmp file.
Second parse the csv and filter the files.
Third is chmod -w.
The next solution stores the files in a tmp
Make a script that gets the servername as a parameter.
See comment in the code:
# Before EDIT:
# Hostname by parameter 1
# Check that you have a hostname
if [ $# -ne 1 ]; then
echo "Usage: $0 hostname"
# Exit script, failure
exit 1
fi
hostname=$1
# Edit, get hostname by system call
hostname=$(hostname)
# Or: hostname=$(hostname -s)
# Additional check
if [ ! -f workfile.csv ]; then
echo "inputfile missing"
exit 1
fi
# After edits, ${hostname} is now filled.
find / -xdev -type f -perm -002 -name "${file}" > /tmp/allfiles.tmp
# Do not use cat workfile.csv | grep ..., you do not need to call cat
# grep with ^ for beginning of line, add a , for a complete first field
# grep "^${hostname}," workfile.csv
# cut for selecting second field with delimiter ','
# cut -d"," -f2
# while read file => can be improved with xargs but lets start with this.
grep "^${hostname}," workfile.csv | cut -d"," -f2 | while read file; do
# Using sed with #, not /, since you need / in the search string
# Variable in sed mist be outside the single quotes and in double quotes
# Add $ after the file for end-of-line
# delete the line with the file (#searchstring#d)
sed -i '#/'"${file}"'$#d' /tmp/allfiles.tmp
done
echo "Review /tmp/allfiles.tmp before chmodding all these files"
echo "Delete the echo and exit when you are happy"
# Just an exit for testing
exit
# Using < is for avoiding a call to cat
</tmp/allfiles.tmp xargs chmod -w
It might be easier when you can chmod -w all the files and chmod +w the files in the csv. This is a little different than you asked, since all files from the csv are writable after this process, maybe you do not want that.

Unique file names in a directory in unix

I have a capture file in a directory in which some logs are being written in a file
word.cap
now there is a script in which when its size becomes exactly 1.6Gb then it clears itself and prepares files in below format in same directory-
word.cap.COB2T_1389889231
word.cap.COB2T_1389958275
word.cap.COB2T_1390035286
word.cap.COB2T_1390132825
word.cap.COB2T_1390213719
Now i want to pick all these files in a script one by one and want to perform some actions.
my script is-
today=`date +%d_%m_%y`
grep -E '^IPaddress|^Node' /var/rawcap/word.cap.COB2T* | awk '{print $3}' >> snmp$today.txt
sort -u snmp$today.txt > snmp_final_$today.txt
so, what should i write to pick all file names of above mentioned format one by one as i will place this script in crontab,but i don't want to read main word.cap file as that is being edited.
As per your comment:
Thanks, this is working but i have a small issue in this. There are
some files which are bzipped i.e. word.cap.COB2T_1390213719.bz2, so i
dont want these files in list, so what should be done?
You could add a condition inside the loop:
for file in word.cap.COB2T*; do
if [[ "$file" != *.bz2 ]]; then
# Do something here
echo ${file};
fi
done

How to split a file on "empty line" pattern, into an array of variables ? (only using native bash)

I'm actually using a bash script to work on a deep folder structure, and extracting informations (related-folder size, extracted text from config files, etc ...), to push them into a database to summarize.
"NO NEW PROCESS" is my rule on this script, since every folder leads to about 300 conf files, and I have about 10.000 folders ... so only native command please.
Here's a part of one input file that i'm actually trying to work with :
include_ldap_query
attrs mail
ssl_ciphers ALL
filter (mail=john.doe*)
name MyRequestName1
host myldaphost:30002
use_ssl no
passwd MyPassword
timeout 60
suffix ou=collaborators,ou=My Company,ou=people,dc=MyLdapContent,dc=MyCompany,dc=fr
user uid=MyUserID,ou=accounts,dc=MyLdapContent,dc=MyCompany,dc=fr
ssl_version sslv2
scope sub
select all
include_ldap_query
attrs mail
ssl_ciphers ALL
filter (mail=janedoe*)
name MyRequestName2
host myldaphost:30002
use_ssl no
passwd MyPassword
timeout 60
suffix ou=collaborators,ou=My Company,ou=people,dc=MyLdapContent,dc=MyCompany,dc=fr
user uid=MyUserID,ou=accounts,dc=MyLdapContent,dc=MyCompany,dc=fr
ssl_version sslv3
scope sub
select first
include_ldap_query
attrs mail
ssl_ciphers ALL
filter (mail=jimmy.page*)
name MyRequestName3
host myldaphost:30002
use_ssl no
passwd MyPassword
timeout 60
suffix ou=collaborators,ou=My Company,ou=people,dc=MyLdapContent,dc=MyCompany,dc=fr
user uid=MyUserID,ou=accounts,dc=MyLdapContent,dc=MyCompany,dc=fr
ssl_version sslv3
scope sub
I'd like to put those querys into an array, to work separately with each other.
how can i split on the empty line pattern ?
Not exactly pure BASH but you can use null RS in awk for this splitting on an empty line:
awk '{print NR ":", $0}' RS= file
Pure bash
You might use a blank line to trigger an array index increment.
In this example we assume the input is piped to stdin.
The layout in the array will be like:
ARRAY[0]="include_ldap_query |attrs mail|ssl_ciphers ALL|filter (mail=john.doe*)|..."
ARRAY[1]="include_ldap_query |attrs mail|ssl_ciphers ALL|filter (mail=janedoe*)|...."
This could do the job, i think.
#!/bin/bash
INDEX=0
while read LINE
do
[ "$LINE" = "" ] && (( ++INDEX )) || ARRAY[$INDEX]="$ARRAY[$INDEX]""|$LINE"
done
# down here comes the rest of your code
# everything is in arrays by now

Resources