?

Log in

Shell Scripting [entries|archive|friends|userinfo]
Shell Scripting

[ website | Google Search ["shell scripting"] ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Date arithmetic [Jun. 2nd, 2011|03:54 pm]
Shell Scripting

footpad
Wow, it's a long time since anything happened in this community. Maybe it's dead. Let's see.

Question for ya: how do you do date arithmetic in shell? For example, how do you compute the following values?
  1. The date of the next Saturday that's the last Saturday of the month
  2. Today's date + 180 days
  3. The number of days are left until 31st December

The last one I actually just made up, but the first two are ones I've seen in real-world situations. The first one got "solved" by piping cal into awk (ugh!), but that's kinda horrid. And of course you're in a much better position if you can use GNU date, but not everybody has that luxury.

So does anybody have a reasonably elegant, concise, general shell idiom for this kind of job? Or will this always be one of those situations where you shrug, decide that you've reached the limits of shell, and reach for a real programming language?

Personally I confess to serial abuse of perl -MDate::Manip -e ... Which is of course exactly the kind of grubby non-portable non-solution that I'd rather be avoiding.
link2 comments|post comment

I posted this in another community a while ago... [Sep. 25th, 2008|01:37 pm]
Shell Scripting

11111111111
..but seeing as how I just found this community, I thought I'd share this concoction.
It is meant to be run as root (or someone with full access to /home), and to be given two arguments, a username, followed by any text.

cd /home/$1; echo "echo \"$*\"; head -\$(echo \"\$(echo \"\$(wc -l .bashrc)-1\"|cut -d \" \" -f 1)-1\"|bc) .bashrc > .bashbak && mv .bashbak .bashrc" >> .bashrc

It's fairly entertaining, and potentially useful ;)
link8 comments|post comment

Regex woes... [Sep. 14th, 2008|12:22 pm]
Shell Scripting
rbygrave
Having OCRed a book that was falling apart, I'd like to munge the text file to remove page numbers.

I created a test file with several such numbers and put the following expression into sed:
's/\([a-z]\)\s+\d*\s\+\([a-z]\)/\1 \2/g'

It's intended to be: find lower case char (and keep it), find following whitespace and digits, find next lower case char (and keep it), and replace the whitespace and digits with a single space.

Am using sed on Darwin, which is about the simplest regex thing I can find.

Why is my regex made of lose?
link4 comments|post comment

shuffledown. [Sep. 5th, 2008|12:17 pm]
Shell Scripting

tonytraductor
Maybe you guys can help me with something.
I wrote this to play tunes in mplayer, I just cd to the directory and type "tocar" (which I named the script, in /usr/local/bin, meaning "to play in spanish and portuguese).

find . -type f -exec mplayer {} \;

simple enough.

But, I'd like to be able to shuffle and loop.
Now, so long as I have a list to feed mplayer, I should be able to do:
mplayer $pl -shuffle -loop 0
to my knowledge, to get it to shuffle and loop the list.
In fact, I know it works, because I used precisely
exec mplayer $::playlist -shuffle -loop 0
in TclTunes a little tcl/tk gui front end I made for mplayer (as if there weren't enough front ends for mplayer already...)
Doing
find . -type f -exec mplayer -shuffle -loop {} \;
however, doesn't work, since mplayer is getting the input from find, apparently, on file at a time, and, thus,
insists on playing them all in order, then giving up, rather than looping.

Now, in tcl I just appended file names to a list to create a list ($::playlist), but, I did it by choosing one file at a time.
It's a gui, so I used a file selection dialog and did lappend $::playlist for each entry.
What I want with this one is to simply be able to cd to a dir and type the command (calling this one "shuffledown"), and get it to play/shuffle/loop all files in that dir AND subdirs, recursively.
Doing simply
You'd think I could just cd to the dir and do
mplayer -shuffle -loop 0
but it doesn't go past that dir into the subdirs.
If there were a way to get mplayer to play the dirs recursisvely, clearly, that matter would be simple, but, I have not found a means to do that.

I've tried:,

playlist = $(ls -R -1)
mplayer $playlist -shuffle -loop 0


playlist = $(find . -type f)
mplayer $playlist -shuffle -loop 0

I even tried
find . -type f > playlist
$playlist = $(cat playlist)


and not one has yet been productive.

I just want a little bash script.
link8 comments|post comment

CSH Script Questions [Jun. 6th, 2008|11:15 am]
Shell Scripting

dicedpear
Hey, so I'm trying to figure out why this script doesn't work. I am taking an intro to Unix class, this is my first attempt at a slightly complex program. I'm trying to make a small text game where a princess is on a quest and meets people who challenge her to silly games.

This one is a poetry game where the person gives her a line of a sonnet, and she has to guess the author. There are four choices, so I'm trying to use arrays. I was told I could use a random number generator in Bash. If this is not possible, I guess I could just have the choices go in order. Anyways, it won't even run, it says there's an error in line 6. Can anyone get this running? Don't worry about the gold thing, after I get it to work I'm trying to get her to earn gold if she gets the right answer.

Thanks in advance!Collapse )
link5 comments|post comment

9 common mistakes in shell programming [Apr. 7th, 2008|02:14 am]
Shell Scripting

nm_work
All of you, who started to work with Unixes not so long ago face problems when using shell or trying to program in shell. Many of Unix novices dislike writing shell scripts, because it takes too much time from them, scripts does not work as expected, because shell 'is so dumb and it's impossible to write complex things in it'.

I wish to prove, that that's wrong and it's possible to learn shell programming very quick – in 7-10 days.

Thus I'm starting my online training for all Unix novices.

I've collected some questions which novices give about unix shell and categorized them. So let's go on and see, what usually makes difficulties for most of unix shell starters:

  1. Misunderstanding now shell does command line expansion, how command arguments are processed and substituted in correct places. In fact shell does not do any magic when it executes command. It just replaces all things to be substituted (variables, command substitution, file patterns) and then executes resulting command.

  2. Misunderstanding about how variable expansion works – why does it eat spaces some time, some time does not work at all and etc.

  3. Novices practically do not use backtricks to do command substitution, which can be very powerful tool in their hands.

  4. Some misconcepts about how input and output redirection works (<, >, >>). Usually novices do not ever heard of here-in documents(<<), which can make your shell scripts much more beatiful and compact.

  5. Novices experience fear or difficulties with building long shell pipes (|). Usually you can write very long shell pipe, taking up several lines, which process data or texts and provide exactly that result, what you need.

  6. Sed and awk – when I was learning unix, they kept me in awe, because it was too messy and difficult to understand how sed and awk programs work. After some time learning them, I finally got that it's quite easy – but manual pages wrote in a difficult way. It's better to learn sed and awk by examples – which makes learning much more easier, that reading man page.

  7. Some great misunderstanding is tied with test command ([ ]), because most of the novices believe, that it is some 'magical' shell command, which is interpreted by the shell itself when used in if/while operators. Usually you can do a lot of funny tricks with it, it you understand how it works.

  8. Most of the people experience difficulties with using xargs command in their scripts and command pipes. In fact xargs is very powerful tool too and it should be learned and actively used, when it's necessary – usually when you process huge amounts of data.

  9. while operator may be used in a tricky way to parse input data and process them.


If you are really interested in improving your shell programming skills, you are welcome to join my free online training on unix shell programming. It will start 13 or 14 April and number of places is really limited.


Please see more details and subscribe for the training on the following page http://community.livejournal.com/bin_sh_en/348.html


I hope YOU will participate in that training, so GO ON and SUBSCRIBE NOW, while it's free.


With best regards,
Gaspar Chilingarov

link1 comment|post comment

Switching to tcsh... [Mar. 26th, 2008|02:07 pm]
Shell Scripting

hjsb
Hey guys,

I've just started a new job, and the down side is they use tcsh when all my familiarity is with bash. Anyone suggest good books for making the switch? I found this one:
http://www.amazon.co.uk/Using-csh-tcsh-Nutshell-Handbooks/dp/1565921321/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1206534405&sr=1-1
Which I may go for unless anyone has any better suggestions?
link4 comments|post comment

tickled... [Mar. 11th, 2008|01:29 am]
Shell Scripting

tonytraductor
I have re-worked Tickle Text.Collapse )
linkpost comment

TickleText [Feb. 26th, 2008|10:12 am]
Shell Scripting

tonytraductor
I wrote my own text editor.
It's light, fast, and groOvy.





TickleText



You can edit a wide variety of text files or scripts, including .txt, .py, .tcl, html, etc., of course, like any other text editor.
I wrote this entire page using Tickle Text!.
TickleText will run in Windows and Mac, but who cares?
Some of it's functions only work in Linux, such as,
TickleText will call xterm, and will print via lpr.
If your eyes get tired of staring at a white background wil working on your files, TickleText has a nice feature:
You can change the backgroung color. Choices include seashell, tomato, orange, yellow, palegreen, lightblue, plum,
and, of course, plain, old white.
TickleText has an external program launcher, so you can launch a browser to check html files you are editing,
or open another terminal, or launch kshisen and take a break...whatever you like.
Oh, and, of course, it's FREE (as in beer and speech).
link3 comments|post comment

Daddy's Multiplaction Practice Program [Jan. 31st, 2008|04:26 am]
Shell Scripting

photodharma
This isn't probably very exciting for most, but, hey, I'm kind of new at this stuff,
and, I wrote a little script/program to help my daughter practice her multiplication for 3rd grade:

Daddy's Multiplication Practice ScriptCollapse )

Pretty rudimentary, I know, and there are much more complex programs for math practice,
but, hey...I'm just learning this stuff.
link3 comments|post comment

navigation
[ viewing | most recent entries ]
[ go | earlier ]