LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Blogs
User Name
Password

Notices


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 149 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 161 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 423 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 81 Comments 0 HMW is offline
Old

Make a bash script recognize bash aliases

Posted 05-12-2015 at 04:16 PM by the dsc (linux-related notes)
Updated 05-12-2015 at 04:22 PM by the dsc (mismatched tags)

Say, if you have a script that would take as a parameter something that you have aliased, it wouldn't work.

I mean:

$ ./script.sh myalias whatever else
/home/dude/scripts/script.sh: line 3: myalias: command not found



In order to make it work, the said script must include:

Code:
shopt -s expand_aliases
source ~/.bash_aliases
(Or wherever your aliases are set, which would preferably be...
Member
Posted in Uncategorized
Views 510 Comments 0 the dsc is offline

  



All times are GMT -5. The time now is 02:09 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