LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs
User Name
Password

Notices


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 1142 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 3257 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 2668 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 1455 Comments 0 the dsc is offline
Old

[bash] "keep trying" command, even if exits with error, for at least 10 times

Posted 08-08-2014 at 10:40 PM by the dsc (linux-related notes)
Updated 08-08-2014 at 10:42 PM by the dsc
Tags bash

A script that tries to run a given arbitrary command N times, or until it exits with success. The commented parts are aborted attempts to make it figure whether the first parameter is a number instead of a command, and that would be used as the maximum number of attempts. Something went wrong, sometime I'll try to figure it out.

Quote:
#!/bin/bash
#input="$@"
# this program is stupid. It's declared to be in public domain by its author, who whishes to remain anonymous
...
Member
Posted in Uncategorized
Views 910 Comments 3 the dsc is offline

  



All times are GMT -5. The time now is 09:21 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
Open Source Consulting | Domain Registration