LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Blogs
User Name
Password

Notices


Old

Bash variables storing the output of commands run them at each variable statement, not just at some eventual invocation

Posted 08-10-2016 at 04:30 PM by the dsc (linux-related notes)
Updated 08-10-2016 at 04:33 PM by the dsc
Tags bash

Meaning that a script line such as:

nowstatus=`mpc | grep '#' | cut -d '/' -f1`

Is running mpc, grep and cut at its very statement, not only from an eventual "echo $nowstatus".

Therefore, in scripts that parse differently the same output of a given command, it may be preferable to first store this output in a temporary file, and then parse this file rather than running the same command several times. Such as in the lines added below their equivalent...
Member
Posted in Uncategorized
Views 100 Comments 0 the dsc is offline
Old

N00b method to toggle some verbosity on command for bash scripts

Posted 03-28-2016 at 11:32 PM by the dsc (linux-related notes)
Updated 03-28-2016 at 11:34 PM by the dsc
Tags bash, newbie

Just something I came up with that is probably not how actual programmers/scripters(?) would advise, specially the parameter/flag thing which is beside the point here.

Code:
if [[ $1 == "v" ]] ; then

ver(){
echo $@
}

else

ver(){
:    # ":" is a "do nothing" command
}

fi

a=10

echo normal output

ver verbose output $a
...
Member
Posted in Uncategorized
Views 162 Comments 0 the dsc is offline
Old

New file name generator script/function, to avoid overwrites w/o user input

Posted 03-27-2016 at 07:11 PM by the dsc (linux-related notes)

Usage examples:

cp/mv/ln/etc file11.txt $(newname "/some/path/file11.txt")

cp/mv/ln/etc file11.txt $(newname "/some/path/whatever.txt")


If there's a file11.txt or whatever.txt already there, it will rename automatically to file11(1).txt or whatever(1).txt, and (N+1) from then on.

The whole path/name must be between quotes because of spaces and such things.

May be dangerous and somehow make you lose files...
Member
Posted in Uncategorized
Views 173 Comments 0 the dsc is offline
Old

Parsing script arguments in bash, without getopt(s)

Posted 03-12-2016 at 05:15 PM by the dsc (linux-related notes)
Tags arguments, bash

Nifty. Just found that on stackoverflow:

http://stackoverflow.com/questions/1...uments-in-bash

Quote:
Originally Posted by Bruno Bronosky, cwd, et al.
Preferred Method: Using straight bash without getopt[s]

I originally answered the question as the OP asked. This Q/A is getting a lot of attention, so I should also offer the non-magic way to do this. I'm going to expand upon guneysus's answer to fix the nasty sed and include Tobias Kienzler's suggestion.

Two
...
Member
Posted in Uncategorized
Views 468 Comments 2 the dsc is offline
Old

Bash arrays as Python lists (sort of)

Posted 08-08-2015 at 11:38 AM by HMW
Updated 08-08-2015 at 11:44 AM by HMW

If you have some experience with higher level programming languages (such as Python for instance), you have undoubtedly came across lists and arrays.

There are arrays in Bash too, but there is a pitfall when you delete items from an array in Bash; the index disappears with the item!

First, consider this example with a list in Python:
Code:
>>> myList = ['foo', 'bar', 'baz']
>>> myList
['foo', 'bar', 'baz']
>>> del myList[0]
...
HMW
Member
Posted in Uncategorized
Views 98 Comments 0 HMW is offline

  



All times are GMT -5. The time now is 04:54 PM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration