Post to Friendica with bash, curl and vim
Mar. 9th, 2012 08:57 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
This script allows you to post to friendica, with xposting options, using bash, curl and vim.
find me on friendica at tony@free-haven.org.
see more of my scripts, in bash, tcl, python, and more, at http://tonybaldwin.me/hax
#!/bin/bash
# update friendica with bash, vim and curl
# I put this in my path as "friendi.sh"
# by tony baldwn, http://tonybaldwin.me
# on friendica at http://free-haven.org/profile/tony
# released according to the Gnu Public License, v.3
# first, create a post/update
filedate=$(date +%m%d%y%H%M%S)
# if you did not enter text for update, the script asks for it
if [[ $(echo $*) ]]; then
ud="$*"
else
vim $filedate.fpost
ud=$(cat $filedate.fpost)
fi
# now to see if you want to crosspost elsewhere
echo "For the following question regarding crossposting, please enter the number 1 for yes, and 0 for no."
echo "If your friendica has the plugins, and you've configured them, you can crosspost to other blogs and sites."
echo "friendica will even automatically change the bbcode to proper html for you."
echo "would you like to crosspost to "
read -p "statusnet? " snet
read -p "twitter? " twit
read -p "facebook? " fb
read -p "dreamwidth? " dw
read -p "livejournal? " lj
read -p "tumblr? " tum
read -p "posterous? " pos
read -p "wordpress? " wp
# now to authenticate
read -p "Please enter your username: " uname
read -p "Please enter your password: " pwrd
read -p "Enter the domain of your Friendica site (i.e. http://friendica.somesite.net, without trailing /): " url
# and this is the curl command that sends the update to the server
if [[ $(curl -u $uname:$pwrd -d "status=$ud&ljpost_enable=$lj&posterous_enable=$pos&dwpost_enable=$dw&wppost_enable=$wp&tumblr_enable=$tum&facebook_enable=$fb&twitter_enable=$twit&statusnet_enable=$snet&source=friendi.sh" $url/api/statuses/update.xml | grep error) ]]; then
# what does the server say?
echo "Error"
else
echo "Success!"
echo $ud
fi
find me on friendica at tony@free-haven.org.
see more of my scripts, in bash, tcl, python, and more, at http://tonybaldwin.me/hax
An alternative to the pipe (in bash/ksh)
Nov. 6th, 2010 02:07 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Most people in here will probably know how to pipe output from one command to another:
However, what if
It turns out that you can do this in bash and ksh by using the
This will execute
At first this doesn't seem too useful, but this means that you can do nifty things like this:
Which will sort filea.txt and fileb.txt, and then diff the outputs - all without writing a single file.
Note that if the subshells require user input, this isn't going to work, so you can't use this to capture user input and pass it to a script which would otherwise require a filename. However, as long as this isn't the case, everything should work smoothly.
[edit: Oh, and I should mention that,unlike piping, you can execute several commands in the subshell. For example:
This also demonstrates how both the subshell and the main process run simultaneously; the output is "sseneehw", followed by a delay of 2 seconds, followed by "gralb".]
[edit 2: See this comment for an example of how to run multiple commands via piping!]
command 1 | command2
However, what if
command2
doesn't allow reading from standard input, and only supports filenames? How can you do this without writing to a file?It turns out that you can do this in bash and ksh by using the
<(command)
syntax. For example, the above command can be written:command2 <(command1)
This will execute
command1
in a subshell, and at the same time, call command2
with a file descriptor looking something like /dev/fd/63
. When command2
reads from that, it'll get the output of command1
.At first this doesn't seem too useful, but this means that you can do nifty things like this:
diff -u <(sort filea.txt) <(sort fileb.txt)
Which will sort filea.txt and fileb.txt, and then diff the outputs - all without writing a single file.
Note that if the subshells require user input, this isn't going to work, so you can't use this to capture user input and pass it to a script which would otherwise require a filename. However, as long as this isn't the case, everything should work smoothly.
[edit: Oh, and I should mention that,
rev <(echo wheeness; sleep 2; echo blarg)
This also demonstrates how both the subshell and the main process run simultaneously; the output is "sseneehw", followed by a delay of 2 seconds, followed by "gralb".]
[edit 2: See this comment for an example of how to run multiple commands via piping!]
Bash vi mode
Nov. 1st, 2010 01:43 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
I noticed a few people making posts about editing their shell history with vi - so I thought I'd contribute this little trick that someone may or may not have mentioned already:
If you add "set -o vi" to the end of your .bashrc, it changes your command line editing mode to vi keybindings, meaning you can use hjkl to navigate through your history and edit lines using vi oldies but goldies like cw and c$. It's a bit of a pain to get used to at first, but it's good if you're the type who keeps typing cw when you want to change an argument and then get annoyed when it just inserts that as text.
:wq
If you add "set -o vi" to the end of your .bashrc, it changes your command line editing mode to vi keybindings, meaning you can use hjkl to navigate through your history and edit lines using vi oldies but goldies like cw and c$. It's a bit of a pain to get used to at first, but it's good if you're the type who keeps typing cw when you want to change an argument and then get annoyed when it just inserts that as text.
:wq
bash history search
Aug. 3rd, 2010 10:35 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
A quick tip for bash users that I use all the time:
If you have a command in your history that you want to re-execute or edit, but don't want to keep tapping the up key, hit Ctrl-R and then start typing part of the line. bash will find the last line matching your input on the fly. If it's not the one you want, either keep typing until it is, or hit Ctrl-R and bash will show you the next match.
Once you've got it, you have two options: to execute, you can then just press RETURN; to edit, use a movement key (left/right arrows, Home/End, etc) and bash will drop you back to a standard command line with the line you just found.
This trick also works if you know the middle of the line but not the beginning, since bash searches the entire line.
[edited to add: BTW, Fey, do you realise that nobody can add new tags to posts in this community? The only tags in here are ones first used by you. :)]
If you have a command in your history that you want to re-execute or edit, but don't want to keep tapping the up key, hit Ctrl-R and then start typing part of the line. bash will find the last line matching your input on the fly. If it's not the one you want, either keep typing until it is, or hit Ctrl-R and bash will show you the next match.
Once you've got it, you have two options: to execute, you can then just press RETURN; to edit, use a movement key (left/right arrows, Home/End, etc) and bash will drop you back to a standard command line with the line you just found.
This trick also works if you know the middle of the line but not the beginning, since bash searches the entire line.
[edited to add: BTW, Fey, do you realise that nobody can add new tags to posts in this community? The only tags in here are ones first used by you. :)]