SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2010-08-20 17:51:16

johnraff
#!Drunkard
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 2,859
Website

shell script bits & pieces thread

I wondered if it might be worth having a thread for people to post bits of code they wanted to share, but which didn't seem worth starting a whole new thread for. Let's keep this one for shell stuff - bash and dash - though; maybe python, perl, lua... should each have their own thread.

Anyway, to start off, here's a line that's handy to put in the top of a script if you want to make sure it's run in a terminal. For those scripts that need user interaction:

[ -t 0 ] && [ -t 1 ] || { echo "${0}: this script must be run from a terminal" ; exit 1 ;}

This tests if stdin and stdout are connected to a terminal, and exits with an error message if they're not. (So where does the error message go? To ~/.xsession-errors with any luck.)

Last edited by johnraff (2010-08-23 16:36:03)


John
--------------------
( a boring Japan blog , idle twitterings  and GitStuff )
#! forum moderator

Offline

Help fund CrunchBang, donate to the project!

#2 2010-08-21 23:53:29

~HP
#! CrunchBanger
From: Elsaß (fr_FR)
Registered: 2010-01-01
Posts: 187
Website

Re: shell script bits & pieces thread

A solution is to use logger :

log() {
    echo "$1" 1>&2 
    logger -ist "$(basename -- "$0")" "$1"
}

piece of code used in my autostart.sh

Offline

#3 2010-08-23 16:41:40

johnraff
#!Drunkard
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 2,859
Website

Re: shell script bits & pieces thread

^where do you get logger? It's not in the Jaunty repositories.


John
--------------------
( a boring Japan blog , idle twitterings  and GitStuff )
#! forum moderator

Offline

#4 2010-08-23 18:02:18

johnraff
#!Drunkard
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 2,859
Website

Re: shell script bits & pieces thread

another one (feel free to join in smile ):
word counter
I needed to be able to count the words in a cell of a spreadsheet - that's not the sort of thing you seem expected to be doing in spreadsheets (I sometimes get Japanese to translate laid out that way) and eventually I just took the Unix approach and made a little script that counts the words in a selection anywhere, in any application's window. It was very easy, using xsel to get the selection, wc to count the words and zenity to display the result:

#!/bin/bash
# words.sh
# counts words in selection and displays result in zenity window
# dependencies xsel, wc, zenity

text=$(xsel)
words=$(wc -w <<<$text)
zenity --info --title "Word Count" --text "Words in selection:\n${words}\n\n\"${text}\""
exit

Just tie that script to a keyboard shortcut, select some text anywhere on your screen and hit the keyboard. A window will pop up with the word count.


John
--------------------
( a boring Japan blog , idle twitterings  and GitStuff )
#! forum moderator

Offline

#5 2010-08-23 20:49:31

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 9,418

Re: shell script bits & pieces thread

johnraff wrote:

^where do you get logger? It's not in the Jaunty repositories.

I think its in the util-linux package.

Offline

#6 2010-08-24 04:59:32

johnraff
#!Drunkard
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 2,859
Website

Re: shell script bits & pieces thread

^ah I should have just tried 'man logger' before looking for the package roll
That util-linux package has some stuff I use often and a lot I've never heard of but maybe should be using so thanks for that.
logger itself turned out to be in bsdutils, at least on #! 9.04.


John
--------------------
( a boring Japan blog , idle twitterings  and GitStuff )
#! forum moderator

Offline

#7 2010-08-24 05:09:55

johnraff
#!Drunkard
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 2,859
Website

Re: shell script bits & pieces thread

(While you're all collecting your cool stuff ready to post, meanwhile...)
What package does that command come from?
This helped find where 'logger' was. The line comes from commandlinefu.com, a little simplified to drop stuff I didn't want. Add this to your ~/.bash_aliases:

cmdpkg(){ PACKAGE=$(dpkg -S $(which $1) | cut -d':' -f1); echo "[${PACKAGE}]"; dpkg -s "${PACKAGE}" ;}

Then you can run 'cmdpkg logger' in a terminal and eventually (might take a minute) the package will come up. Only works if it's on your system though.


John
--------------------
( a boring Japan blog , idle twitterings  and GitStuff )
#! forum moderator

Offline

#8 2010-08-24 07:29:55

murraythegoz
#! Member
From: Milan Area, Italy
Registered: 2009-04-29
Posts: 77

Re: shell script bits & pieces thread

dos2unix, the perl way

perl -pi -e 's/\r\n/\n/g' *

check for IP address (found somewhere in activestate forums)

(?:1\d?\d?|2(?:[0-4]\d?|[6789]|5[0-5]?)?|[3-9]\d?|0)(?:\.(?:1\d?\d?|2(?:[0-4]\d?|[6789]|5[0-5]?)?|[3-9]\d?|0)){3}

!# statler 10 + eee-control running on eee701/4gb/512ram
!# statler/sid running on MacBookAir 4,1 (mid 2011) i7-1.8GHz/128SSD/4g RAM

Offline

#9 2010-10-27 16:41:47

johnraff
#!Drunkard
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 2,859
Website

Re: shell script bits & pieces thread

I've got a lot of distro-independent stuff on a separate partition, mounted as /data, with symlinks from my home directory so ~/documents is really a symlink to /data/john/documents. If you want to refer to a file or directory in a script it often doesn't matter how you get there, via the symlink or directly, but sometimes it does matter.

You can get the real location of a file with the command 'readlink -f' so, for example

john@raffles3:~$ readlink -f documents
/data/john/documents

Comes in handy sometimes.


John
--------------------
( a boring Japan blog , idle twitterings  and GitStuff )
#! forum moderator

Offline

#10 2010-12-02 11:33:44

johnraff
#!Drunkard
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 2,859
Website

Re: shell script bits & pieces thread

apostrophes inside single quotes
Something I just learned recently:
Single quotes are quite useful sometimes; they don't expand dollar signs, backslashes or anything so you can put whatever you want in them and it will be passed on just like that. With the exception of single quotes/apostrophes themselves of course. For example

'What's this?'

will be taken as a quoted string:
'What'
then an unquoted string:
s this?
then a quote by itself... so it won't work. Backslashes inside single quotes don't work either so you can't just escape it that way. However, what you can do is break open the quoted string at the point where you want the apostrophe, put it in, then start quoting again. Like:

'first bit' <apostrophe> 'second bit'

OK, but the apostrophe is now exposed to the shell so still needs some protection - either a backslash or surrounding it with double quotes will work here. So for the first example these both work:

'What'\''s this?'
'What'"'"'s this?'

wink


John
--------------------
( a boring Japan blog , idle twitterings  and GitStuff )
#! forum moderator

Offline

#11 2010-12-05 01:36:42

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 9,418

Re: shell script bits & pieces thread

I stole this script from SystemRescueCD (I added the sudo though):

#!/bin/sh
echo '========================>>> fsarchiver probe simple <<<========================'
sudo fsarchiver probe simple
echo ' '
echo 'Press a key to continue'
read key

It requires fsarchiver and it gives you a nice text output of your disks and partitions:
lJvv9.png

Offline

#12 2010-12-07 04:55:34

johnraff
#!Drunkard
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 2,859
Website

Re: shell script bits & pieces thread

Two recently found gems from the excellent commandlinefu:

Remove duplicate lines from a file:

awk '!a[$0]++' file.txt

Explanation #43 here.

Read a random line from a file:

shuf -n1 file.txt

Explanation 'man shuf'. smile

------------------------------------------------------

Found these via the terminal search one-liner. I've posted it before somewhere but it's so nice, again - put this line in your .bash_aliases:

cmdfu(){ wget -qO - "http://www.commandlinefu.com/commands/matching/$@/$(echo -n "$@" | openssl base64)/plaintext"; }

and you can do eg 'cmdfu random line' to find the last one. cool


John
--------------------
( a boring Japan blog , idle twitterings  and GitStuff )
#! forum moderator

Offline

#13 2010-12-07 05:47:32

Chewie
#! CrunchBanger
From: /dev/null
Registered: 2010-12-01
Posts: 103

Re: shell script bits & pieces thread

anonymous wrote:

I stole this script from SystemRescueCD (I added the sudo though):

#!/bin/sh
echo '========================>>> fsarchiver probe simple <<<========================'
sudo fsarchiver probe simple
echo ' '
echo 'Press a key to continue'
read key

It requires fsarchiver and it gives you a nice text output of your disks and partitions:
lJvv9.png

I'm diggin' it. Thank you. =]


#!CrunchBang Linux ~$ apt-get into it | #!(Statler:R20101205): OpenBox | Like To Program?
                          “The cure for boredom is curiosity. There is no cure for curiosity.”

Offline

#14 2010-12-08 17:07:21

johnraff
#!Drunkard
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 2,859
Website

Re: shell script bits & pieces thread

^looks good but fsarchiver isn't in the Jaunty repos. I really have to make the shift to Statler here soon - starting to feel left behind. neutral


John
--------------------
( a boring Japan blog , idle twitterings  and GitStuff )
#! forum moderator

Offline

#15 2010-12-09 01:09:34

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 9,418

Re: shell script bits & pieces thread

Well on the bright side what better time to switch to Statler than now with the newly released images.

Offline

#16 2010-12-09 04:23:45

johnraff
#!Drunkard
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 2,859
Website

Re: shell script bits & pieces thread

Yes indeed - time to nuke my defunct Xp partition and put it there. cool


John
--------------------
( a boring Japan blog , idle twitterings  and GitStuff )
#! forum moderator

Offline

#17 2010-12-22 08:22:02

luc
#! Die Hard
From: Munich, Germany
Registered: 2010-03-21
Posts: 561

Re: shell script bits & pieces thread

i have this in my ~/.bash_aliases

comp () { #compare the speed of two commands (loop $1 times)
  if [[ $# -ne 3 ]] ; then return 1 ; fi
  echo -n 1
  time for ((i=0;i<$1;i++)) ; do $2 ; done >/dev/null 2>&1
  echo -n 2
  time for ((i=0;i<$1;i++)) ; do $3 ; done >/dev/null 2>&1
}

and now if I want to see which of two commands is faster i type

comp 100 "command_1 args" "command_2 args"

take care to quote corectly so that "command_1 args" will end up as "$2" etc. (i often define little functions f1, f2 interactively to test them against each other)

luc

Offline

#18 2010-12-22 11:51:01

Sector11
77345 ¡#
From: SR11 Cockpit
Registered: 2010-05-05
Posts: 11,691
Website

Re: shell script bits & pieces thread

johnraff wrote:

I wondered if it might be worth having a thread for people to post bits of code they wanted to share, but which didn't seem worth starting a whole new thread for. Let's keep this one for shell stuff - bash and dash - though; maybe python, perl, lua... should each have their own thread.

Anyway, to start off, here's a line that's handy to put in the top of a script if you want to make sure it's run in a terminal. For those scripts that need user interaction:

[ -t 0 ] && [ -t 1 ] || { echo "${0}: this script must be run from a terminal" ; exit 1 ;}

This tests if stdin and stdout are connected to a terminal, and exits with an error message if they're not. (So where does the error message go? To ~/.xsession-errors with any luck.)

Any way to get it to use: zenity?


#! Forum Moderator | To #! or not to #!?  That is the question!
#! Conky = 16 Threads & the Conky Archives | Conky PitStop | OHCG'er
StartPage -  protect your privacy   |   BBQLinux

Offline

#19 2010-12-22 13:12:16

mobilediesel
#! Member
Registered: 2010-08-19
Posts: 73

Re: shell script bits & pieces thread

Sector11 wrote:
johnraff wrote:

I wondered if it might be worth having a thread for people to post bits of code they wanted to share, but which didn't seem worth starting a whole new thread for. Let's keep this one for shell stuff - bash and dash - though; maybe python, perl, lua... should each have their own thread.

Anyway, to start off, here's a line that's handy to put in the top of a script if you want to make sure it's run in a terminal. For those scripts that need user interaction:

[ -t 0 ] && [ -t 1 ] || { echo "${0}: this script must be run from a terminal" ; exit 1 ;}

This tests if stdin and stdout are connected to a terminal, and exits with an error message if they're not. (So where does the error message go? To ~/.xsession-errors with any luck.)

Any way to get it to use: zenity?

This should do it:

[ -t 0 ] && [ -t 1 ] || { zenity --warning --text="${0}: this script must be run from a terminal." ; exit 1 ;}

Offline

#20 2010-12-22 15:06:13

Sector11
77345 ¡#
From: SR11 Cockpit
Registered: 2010-05-05
Posts: 11,691
Website

Re: shell script bits & pieces thread

anonymous wrote:

I stole this script from SystemRescueCD (I added the sudo though):

#!/bin/sh
echo '========================>>> fsarchiver probe simple <<<========================'
sudo fsarchiver probe simple
echo ' '
echo 'Press a key to continue'
read key

It requires fsarchiver and it gives you a nice text output of your disks and partitions:
lJvv9.png

This I like, but how come I don't see something like this:

[sudo] password for sector11: 

in yours?

Last edited by Sector11 (2010-12-22 15:07:28)


#! Forum Moderator | To #! or not to #!?  That is the question!
#! Conky = 16 Threads & the Conky Archives | Conky PitStop | OHCG'er
StartPage -  protect your privacy   |   BBQLinux

Offline

#21 2010-12-22 15:12:34

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 9,418

Re: shell script bits & pieces thread

Its because for some reason I setup sudo to not require a password for anything. I have now fixed my sudoers file so thank you for pointing that out to me.

Offline

#22 2010-12-22 15:45:42

johnraff
#!Drunkard
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 2,859
Website

Re: shell script bits & pieces thread

luc wrote:

i have this in my ~/.bash_aliases

comp () { #compare the speed of two commands (loop $1 times)
  if [[ $# -ne 3 ]] ; then return 1 ; fi
  echo -n 1
  time for ((i=0;i<$1;i++)) ; do $2 ; done >/dev/null 2>&1
  echo -n 2
  time for ((i=0;i<$1;i++)) ; do $3 ; done >/dev/null 2>&1
}

and now if I want to see which of two commands is faster i type

comp 100 "command_1 args" "command_2 args"

take care to quote corectly so that "command_1 args" will end up as "$2" etc. (i often define little functions f1, f2 interactively to test them against each other)luc

That's handy!


John
--------------------
( a boring Japan blog , idle twitterings  and GitStuff )
#! forum moderator

Offline

#23 2010-12-22 15:57:35

Sector11
77345 ¡#
From: SR11 Cockpit
Registered: 2010-05-05
Posts: 11,691
Website

Re: shell script bits & pieces thread

anonymous wrote:

Its because for some reason I setup sudo to not require a password for anything. I have now fixed my sudoers file so thank you for pointing that out to me.

OH!  Well in that case for "clarity" may I suggest:

#!/bin/sh
sudo fsarchiver probe simple
echo ' '
echo '========================>>> fsarchiver probe simple <<<========================'
echo ' '
echo 'Press a key to continue'
read key

To get the sudo stuff above everything and not "in" it.
Also I have no idea why that first:

echo ' '

doesn't create a blank line like that last one does.

  12:59 ~
         $ dnp
[sudo] password for sector11: 
[======DISK======] [=============NAME==============] [====SIZE====] [MAJ] [MIN]
[sda             ] [WDC WD2500AAJS-0               ] [   232.89 GB] [  8] [  0]
[sdb             ] [HM250HI                        ] [   232.89 GB] [  8] [ 16]

[=====DEVICE=====] [==FILESYS==] [======LABEL======] [====SIZE====] [MAJ] [MIN] 
[sda1            ] [ext3       ] [<unknown>        ] [    18.63 GB] [  8] [  1] 
[sda2            ] [ext3       ] [<unknown>        ] [    37.25 GB] [  8] [  2] 
[sda3            ] [swap       ] [<unknown>        ] [     1.96 GB] [  8] [  3] 
[sda5            ] [ext3       ] [<unknown>        ] [    57.74 GB] [  8] [  5] 
[sda6            ] [ext3       ] [<unknown>        ] [    57.74 GB] [  8] [  6] 
[sda7            ] [ext3       ] [<unknown>        ] [    59.56 GB] [  8] [  7] 
[sdb1            ] [ntfs       ] [<unknown>        ] [   232.88 GB] [  8] [ 17] 
 
========================>>> fsarchiver probe simple <<<========================
 
Press a key to continue

Last edited by Sector11 (2010-12-22 15:59:45)


#! Forum Moderator | To #! or not to #!?  That is the question!
#! Conky = 16 Threads & the Conky Archives | Conky PitStop | OHCG'er
StartPage -  protect your privacy   |   BBQLinux

Offline

#24 2010-12-22 16:02:40

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 9,418

Re: shell script bits & pieces thread

You can modify the script however you like. Also in your version, both echo ' ' are working fine. After fsarchiver, you can see two blank lines.

Offline

Be excellent to each other!

#25 2010-12-22 16:16:30

Sector11
77345 ¡#
From: SR11 Cockpit
Registered: 2010-05-05
Posts: 11,691
Website

Re: shell script bits & pieces thread

mobilediesel wrote:
Sector11 wrote:

Any way to get it to use: zenity?

This should do it:

[ -t 0 ] && [ -t 1 ] || { zenity --warning --text="${0}: this script must be run from a terminal." ; exit 1 ;}

And so it was...
thmb_zenity.png

I then ran "mantxt" without an argument - the result was interesting.

mantxt cp

will create /media/5/Documents/cp.txt and open it in gedit, and

mtxt cp

  will open /media/5/Documents/cp.txt  in gedit.


#! Forum Moderator | To #! or not to #!?  That is the question!
#! Conky = 16 Threads & the Conky Archives | Conky PitStop | OHCG'er
StartPage -  protect your privacy   |   BBQLinux

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo