SEARCH

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

You are not logged in.

#51 2012-01-07 23:32:58

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: shell script bits & pieces thread

@johnraff: yes, i figured out those redundant 'CEILING's in the meantime, but thanks anyway smile
as to what it is for:

i am building a script which might seem quite stupid and useless to most. however, i'll explain anyway (and explain my use of it after that). it is best explained by showing you just the input and the (current) output:

[23:44:41]$ word_disrupt disruption
d i s r u p t i o n
2 1 2 1 1 1 1 1 3 3
d d i s s r u p t i o o o n n n
, . - . _ , : , : - - .. .. _

ok, what happens here?
1. the input-word gets spliced into an array of separate letters. in this case, 10 letters.
2. another array (containing the first 4 values of the Fibonnaci-sequence, ie. 1 1 2 3) is read out multiple times, creating an array of 10 numbers (in this case, 1 1 2 3 1 1 2 3 1 1). this array is then randomized to provide the output you see echo'd above. what i'm doing here is assigning a number to each individual letter (not through associative arrays, just two separate ones)
3. the number coupled with each letter determines the amount of times it will be repeated. you can see the result of that action on the 3rd line of output. this new word is 16 letters long.
4. i also have an array filled with some characters (- , . _ : ..). now i use the code i posted above to iterate over this array back and forth, until i have extracted the amount of characters corresponding to the amount of letters (in this case, 16 characters).
5. (this step still needs to be built in, as you can see from the output) - the script will grab a (partially) random amount of characters from that string and distribute them in some way over the word. this will result in something like this (incorrect example): d-diisrupt.tion..

you're probably now going 'wtf? WHY?'.
well, first of all, i like it - disrupted words and stuff. second of all, i like it so much that i once wrote poetry and stuff incorporating these kinds of disrupted words. eventually (probably gonna be a while!) i'd like to incorporate all this into a poetry-writing-script. my detect_syllables-project is another (much much bigger) part of that.

so, i hope that is all clear. to summarize: there is weird stuff going on in my head and i enjoy it and also enjoy scripting so i want to script it in some way. i don't know. being creative here smile

Offline

Help fund CrunchBang, donate to the project!

#52 2012-01-08 04:04:19

aiBo
#! CrunchBanger
Registered: 2010-11-08
Posts: 243

Re: shell script bits & pieces thread

rhowaldt wrote:

there is weird stuff going on in my head and i enjoy it and also enjoy scripting

'nuff said! big_smile

Offline

#53 2012-01-09 15:06:09

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

Yeah this all sounds OK.

Would this be of any help somewhere?

john@raffles3:~$ text='CrunchBang Linux could possibly make your computer go CRUNCH! BANG!'
john@raffles3:~$ translate "$(translate "$text" ja)"
CrunchBang Linux could make your computer go crunch perhaps

Play with the mid-language, though ja is likely to give good surrealism. zh-TW or ko might be good too, or anything remote from English. (Using the translate script.)


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

Offline

#54 2012-01-09 15:31:47

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: shell script bits & pieces thread

^ heh, that looks pretty cool. i might think of using something like that. i still have no clear idea of how all different parts might come together in the end, so any input is always appreciated.

Offline

#55 2012-02-10 18:01:46

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

Multi sed substitutions
I'm not the person to ask about sed (try rhowaldt) but something you often see in scripts where several different substitutions are done is

process | sed 's/string/replacement' | sed 's/otherthing/changetothis/' | ...

Each time you call sed costs you some milliseconds, it looks messy, and anyway long pipelines are a Bad Thing anyone will tell you. You can combine a sequence of commands in one sed call:

process | sed 's/string/replacement;s/otherthing/changetothis/' | ...

(Ask awebb about why too many pipes are bad for you.)

Last edited by johnraff (2012-08-06 16:47:24)


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

Offline

#56 2012-08-06 17:06:55

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

Use tee to fork a command's stdout (and stderr) to a log file, but keep the return value for checking
(This came up in this script recently.)
If you want some command in a script to send its output to a log, while also viewing it in your terminal, you can do

somecommand | tee -a logfile 2>&1

So standard output and error are sent to both logfile and terminal. Unfortunately this stops you from testing the return value of somecommand to see if it exited successfully or not. if you do

somecommand | tee -a logfile 2>&1 || emergencyaction

emergencyaction will never be run even if somecommand fails! The return value of a pipe is the return value of the last command in the pipe, tee in this case, and tee will return 'true' even if somecommand returns 'false'.

You can get round this with process substitution. Put tee in its own substitution ( >( tee ... ) ) and it will look like a file to somecommand which you can redirect standard output (and error if you want) to, while still leaving its return value available to whatever follows the ||. Like this:

somecommand > >( tee -a logfile ) 2>$1 || emergencyaction

cool


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

Offline

#57 2013-02-01 03:53:47

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

The thread is old, but this stuff doesn't go out of date so fast...

I hit a problem yesterday when a script, running as normal user, wanted to check a system folder and take some action if it didn't exist:

[[ -d /path/to/folder ]] || return_error

Unfortunately the folder 'to' belongs to root, and permissions are 700, so the script is not allowed to look inside to see 'folder', so the test always fails.

One way out would be to run the whole script as root but that would cause problems elsewhere, eg every file it made would belong to root...

Another way is to run the command with sudo. However

sudo [[ -d /path/to/folder ]] || return_error

gives

sudo: '[[': command not found

Sudo does not have access to any builtin shell commands, only external calls. Fortunately there is a command 'test' which does the same as the shell builtin '[', so finally this works:

sudo test -d /path/to/folder || return_error

vYTUxZQ


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

Offline

#58 2013-02-02 08:33:15

r0n d0n
New Member
From: 'mericuh
Registered: 2012-11-19
Posts: 8

Re: shell script bits & pieces thread

I've put together this from several sources a while back (can't remember where), and I really only understood it enough to make it work. I've put this in my .bashrc to make it permanent, but you can enter this in the terminal to test it out before you commit.

I may be forgetting some things,  but this will display "user@host : )" in green on the terminal prompt when a command exits with no errors, and the smiley will turn into a ": ( " and become red when the command exits with errors. Also, the current directory is up on the title bar.

export PS1="\[\e]2;\u@\H \W\a\e[32;1m\]\[\e[01;32m\]\u@\h \[\e[01;34m\] \`if [ \$? = 0 ]; then echo -e '\e[01;32m:)'; else echo -e '\e[01;31m:('; fi\` \[\e[01;34m\]$\[\e[00m\]\[\e[0m\] "

>lurk moar

Offline

#59 2013-02-03 15:51:56

pvsage
Internal Affairs
From: North Carolina
Registered: 2009-10-18
Posts: 13,970

Re: shell script bits & pieces thread

Problem:  avconv/ffmpeg puts invalid XING headers on converted mp3 files, both CBR and VBR, resulting in an mp3 that isn't properly searchable on some portable mp3 players and reports incorrect song length in some media player software.

Workaround:  Have avconv/ffmpeg write to temporary file, process through vbrfix, delete temporary file.  (EDIT:  You can specify the same filename for both input and output; vbrfix writes to a temporary file.  This negates the need to remove your initial temporary file.  Also, if you use the -always switch with vbrfix, it will fix CBR mp3s as well.)

Example:

#!/bin/bash
for i in *.mp3
do
  avconv -i "$i" -acodec libmp3lame -aq 0 "${i%.flv}-temp.mp3"
  vbrfix -always "${i%.flv}.mp3" "${i%.flv}.mp3"
done

This is a known bug in avconv/ffmpeg that keeps cropping up; hopefully the developers will nail this down eventually. roll

Last edited by pvsage (2013-02-03 16:24:52)

Offline

#60 2013-06-03 17:58:56

Doomicide
#! Junkie
From: the Nightosphere
Registered: 2011-06-24
Posts: 472
Website

Re: shell script bits & pieces thread

@johnraff: Hint taken, I'll just use this post for some stuff from my mkshrc:

function repeat() {
        for i in {1..${1}}; do
                "${2}"
        done
}


function calc() {
 
        awk "BEGIN{ print $* }"
}

function sprunge() {
        for arg in ${@}; do
                cat $arg | curl -F 'sprunge=<-' http://sprunge.us
        done
}

function trans() {
        lynx -nonumbers -nolist -nolog -dump "http://odict.leo.org/ende?search=$1" \
        | sed -n "/Direct matches/,/[NoaA]*dditional matches$/ { s/\[_\]\s*//; p}" \
        | sed '1d;$d'
}

Last edited by Doomicide (2013-06-04 09:47:51)


“From each according to his faculties; to each according to his needs”
Look at the code. Look at the silly code!

Offline

#61 2013-06-04 04:01:57

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

Thanks Doomicide! A full-blown, and useful, script like this might deserve its own thread in fact, so people can comment or make suggestions...

To be honest, I really envisaged this thread for handy scraps of code that don't really merit their own thread. That said, all contributions are welcome! smile


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

Offline

#62 2013-11-15 03:37:50

cloverskull
#! Junkie
Registered: 2013-10-26
Posts: 377

Re: shell script bits & pieces thread

Old thread, sorry, just found it smile  In my bashrc, I put the following:

eval `dircolors`

That way, when I ssh from another box, the colors are actually readable on a black background.

Offline

#63 2015-12-10 05:16:44

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

This general topic is continued on the BunsenLabs forums.


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

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.
Server: acrobat

Debian Logo