You are not logged in.

#1 2017-03-31 22:30:00

NecoCarmello
Member
Registered: 2016-04-30
Posts: 5

Write script with different output each time run consecutively

Im trying to write a bash script that says (in terminal)
one thing then when u run it again says something else.
I want this to go on maybe 4 or 5 times before it finally finishes.

Note: this script isnt supposed to be usefull just something to practice with and get a few laughs in the process.

Edit: it needs to be run consecutively. If u run another cmd then run it again it should start over.

Last edited by NecoCarmello (2017-03-31 22:31:32)

Offline

#2 2017-03-31 22:35:14

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Write script with different output each time run consecutively

NecoCarmello wrote:

Im trying to write a bash script...

Show us what you have so far.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2017-03-31 23:14:36

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,546
Website

Re: Write script with different output each time run consecutively

It is possible to have different output on a series of executions, but there is no way for a script to know whether something else has run between invocations.

If you're ok with a function, alias, or sourcing the script rather than executing it, then you can access the history.  But an executed script can't access the parent process's history.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2017-03-31 23:21:51

NecoCarmello
Member
Registered: 2016-04-30
Posts: 5

Re: Write script with different output each time run consecutively

Nothing yet iv been google around but couldnt find anything to make it change output. I was thinking a loop maybe but it just said it didnt know what loop was (i may need something like !/bin/bash at the top to use loop)
It just needs to talk back to me like its getting anoyed then finally give in and do something.


P.s. sorry for no code tag on [!/bin/bash] My phones not show the page properly. It has links in my way! sad

Offline

#5 2017-03-31 23:26:41

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Write script with different output each time run consecutively

NecoCarmello wrote:

(i may need something like !/bin/bash at the top to use loop)


You really need to start at the beginning. Read this: http://mywiki.wooledge.org/BashGuide


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2017-03-31 23:26:46

NecoCarmello
Member
Registered: 2016-04-30
Posts: 5

Re: Write script with different output each time run consecutively

Something like the cow in aptitude (not apt-get but aptitudes that says "theres no easter egg here"....blah blah..."i Told u theres no easter egg here" then finally shows the talking cow thats in apt-get

Offline

#7 2017-03-31 23:28:14

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Write script with different output each time run consecutively

Moving to NC...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#8 2017-04-01 00:18:13

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,546
Website

Re: Write script with different output each time run consecutively

Well, I had fun with this at least.  I wrote two functions that access history.  One that keeps a running tally that resets when it's own name wasn't the second to last entry in the history.  The other used a pretty damned ugly but creative invocation of `eval "$(type FunctionName | awk '...')"` as a working proof-of-principle of self-modifying functions in bash.  I've done self-modifying scripts before, that's much easier, but modifying shell functions at run time is a bit more interesting.

However, both depend on HISTCONTROL not containing ignoredups, otherwise there is no way to infer from shell history how many times the function had been called.  The self-modifying function was my attempt to work around this as it would only really need to know if the last command was the the self-modifying function.  Unfortunately, when this is checked from *within* the function, the function name has already been appended to the history, so it's too late: the name of the function will be the last item in history whether it was just called once or many times.

That said, you clearly shouldn't be bothering with anything nearly this interesting complicated.  Follow the tutorial linked above and learn the basics first.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#9 2017-04-01 02:53:01

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,475

Re: Write script with different output each time run consecutively

Just an idea:

Script A reads and echoes first uncommented line of file B, and after doing that successufully it will comment-out the first line of B before exiting.
When run second time, it will echo the second line as first line is already commented-out, and before exit it will comment-out the second line(that is first uncommented line).
When there are no uncommented lines in file B, the script will remove all # from all lines and it will starts fresh.

Offline

#10 2017-04-01 02:58:55

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,546
Website

Re: Write script with different output each time run consecutively

Docbroke, that completely skips the criteria of starting fresh if it wasn't run sequentially (e.g. any other command was run).  Without that criteria this is trivial and there is no need for commenting out lines in another file.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#11 2017-04-01 11:38:17

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,892

Re: Write script with different output each time run consecutively

This sounds like we're doing some guys homework ;-P

I'd skip the history (because that can be affected from various shells and use the timestamp of a /tmp/.$USER.naughty.loop to propell or reset the loop (w/ the loops iteration stored in that file)

If however the history is relevant (maybe because it's mentioned in some task description ... smile you'll have to go for an function which inspects the history contents and simply writes the loop iteration into a variable.

One problem here is that the history implementations among shells are about as compatible as a square peg and a round hole.
"history" will print a randomly sized tail of the history, but that's all you can half-wise rely on, so you'll resort to "fc -ln -1".

What's worse is the behavior on editing the history, eg. when ignoredups, zsh will wipe the function/alias name from the top of the history stack *while* it's executed, ie. you've no chance to figure that it was the last command - while bash, as Trilby mentioned, presents us with the opposing problem.

This should work on bash, but not zsh ...

naughty() { LAST_TOKEN=$_; if [ "$LAST_TOKEN" = "naughty" ]; then echo 'EBKAC #'$((NAUGHTY_LEVEL++)); else echo "go away"; NAUGHTY_LEVEL=0; fi  }

Offline

#12 2017-04-01 12:17:01

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,546
Website

Re: Write script with different output each time run consecutively

Seth, that's close to my first function, but yours only works when the function has no parameters, no whitespace, etc.  It will fail otherwise.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

Board footer

Powered by FluxBB