//Tips tagged read
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
I wrote a small script, I named it "tw" to update twitter from terminal:
This can also be used to update identi.ca (the open twitter alternative) by replacing the twitter url with 'http://identi.ca/api/statuses/update.xml'.
#!/bin/bash
echo "Your message please..."
read MSG
echo $MSG > characters
echo "Message length"
wc -c characters
echo "Password please..."
read -s PW
wget --keep-session-cookies --http-user=your.email@address.here --http-password=$PW \
--post-data="status=$MSG" \
http://twitter.com:80/statuses/update.xml
echo "Message posted."
This can also be used to update identi.ca (the open twitter alternative) by replacing the twitter url with 'http://identi.ca/api/statuses/update.xml'.
In bash scripting if you have a situation where you don't want to wait forever for a user to respond, you can use the read command with the -t option which causes read to time out in "number of seconds" specified.
From read command man page:
-t timeout : Cause read to time out and return failure if a complete line of input is not read within timeout seconds. This option has no effect if read is not reading input from the terminal or a pipe.
-p prompt : Display prompt, without a trailing newline, before attempting to read any input. The prompt is displayed only if input is coming from a terminal.
Example:
This is also useful as a replacement for the sleep command as it can be cancelled more easily. For example instead of 'sleep 60':
From read command man page:
-t timeout : Cause read to time out and return failure if a complete line of input is not read within timeout seconds. This option has no effect if read is not reading input from the terminal or a pipe.
-p prompt : Display prompt, without a trailing newline, before attempting to read any input. The prompt is displayed only if input is coming from a terminal.
Example:
$ ans="y" $ read -t 5 -p "Want to proceed ?(y/n)" ans; echo "You entered $ans"
This is also useful as a replacement for the sleep command as it can be cancelled more easily. For example instead of 'sleep 60':
read -t 60 -p "Press enter to continue"
The 'read' command can be used to split arguments based on any arbitrary character using IFS:
$ echo 'a/bc/ de fg hi/j' | { IFS='/' read first second others; echo "$first"; echo "$second"; echo "$others"; }
a
bc
de fg hi/j

