BASH: Change alias to script to add a task


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers BASH: Change alias to script to add a task
# 1  
Old 01-17-2011
BASH: Change alias to script to add a task

Hi.

I use an alias, "homeperm" as shorthand for curl -o. Since most of what I download via cUrl is graphic image files -- jpeg files -- I'd like to be able to change this alias to a script, or use it to invoke a function, which will not only download the file but date-stamp it using Exiv2 in either (or both) the IPTC or EXIF annotation "blocks". This would save me the hassle of going back and manually entering this information after reading the mod date for file X or Y in my file manager.

if anyone is familiar with the "curl -o" command, and I suppose not a few people are, then you know that its syntax is [command][your-file-name][source-URL]. Where I'm drawing a blank in turning this alias into something fancier is: I'm not sure, having neither seen it done nor done it myself in the past, how to write a script or function that will follow this same syntax.

I've done some simple scripts that prompt for input using the echo and read commands, but what I'm looking for is a way to enter the [your-file-name] and [source-URL] parts on the same line with the "homeperm," which is how the alias works and is very much what I've grown accustomed to.

Is it possible in BASH to get the second and third parts of a string from stdin passed, just as a f'r'instance, as variable values that can be handled by a function or script like this? If so, how would one go about it?

Or is it necessary to already have a subshell running before such a thing could be done?

I'm sorry I can't offer any "crappy candidate code" to show what I mean, but if anyone needs part or all of this idea explained any more clearly, then please ask: I'll do my best.

Hoping someone can help me with this.

BZT
# 2  
Old 01-17-2011
I dont get what you mean by "enter the [your-file-name] and [source-URL] parts on the same line with the "homeperm"
This User Gave Thanks to hongwei For This Post:
# 3  
Old 01-18-2011
The alias works like this:
Code:
homeperm bigger-than-life-xhd.jpg http://www.wd.com/store/drives/excalibur-premium-26tb-portable-sata-hd.jpg

(just something off the top of my head)

You'll notice that the name I'm giving the file and it's source URL are on the same line as the alias. I'm positive that this is because aliases, by definition, often substitute one word for two, or are used in place of particular commands that are easily confused with others the same binary might employ. cUrl also has, on its man page, a curl -O command -- the original reason for creating this alias was that I couldn't remember which "o" did what.

Does this explain it adequately?

BZT

---------- Post updated at 14:02 ---------- Previous update was at 13:57 ----------

I know an alias invoking a function or script can be made to perform a simple task on a local file. I have another alias, ditto, that starts a script called mditto, the code of which looks like this:
Code:
	d=$(exiv2 -g Iptc.Application2.FixtureId -Pv $1 2>/dev/null)
	if [[ "d" != "" ]]; then
		exiv2 -M"set Iptc.Application2.ObjectName $d" -k $1
		echo -e "RUBBERSTAMP: $1; '$d' to ObjectName field"
		echo -e "Thank Irfan for me next time you see him!"
	else
		echo -ne "Ditto what? There's no Fixture ID data in $1."
		echo -ne "I'll pass on this one thanks."
	fi

"$1" is the variable (placeholder?) for the file to be worked on. If I had a "$1" with a value of
Quote:
bigger-than-life-xhd.jpg http://www.wd.com/store/drives/excalibur-premium-26tb-portable-sata-hd.jpg
...I suppose I could use 'cut' or a string builtin to break it into two variables at the space, give those values to cUrl, stat (for) the date on "bigger-than-life-xhd.jpg" and pass the mod date from that to Exiv2 to annotate the file.

Does it sound like I'm on the right track? I'll try some code in a short while, test it, and if I run into a wall I'll be back round to ask where I'm coming up short.

BZT
# 4  
Old 01-18-2011
If you want to use parameters, a function would be better than an alias. Not all shells have them but bash definitely does. You can use them like little callable scripts you put in your .bashrc

Code:
function homeperm
{
        local LOCALFILE="$1"
        local URL="$2"

        do_stuff_with_parameters
}

...and you'd just run it as homeperm parameter1 parameter2

And no, you never need to use 'cut' to do something as simple as that, the read command can do a lot for you:

Code:
$ read A B <<< "c d"
$ echo $A
c
$ echo $B
d
$

You can also split on something other than whitespace:
Code:
$ IFS="|" read A B <<< "c d|e"
$ echo $A
c d
$ echo $B
e
$

It's much more efficient than calling cut since read's a builtin and always a builtin.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-18-2011
MySQL Found another way to do it.

I appreciate the suggestions. Likely as not I'll be coming up with some scripts very much like this one in the near future, so I'll keep this thread bookmarked.

What I failed to mention is, while I write the filenames I want into the "homeperm" or "curl -o" command, I usually paste in the source URLs from the clipboard of whatever OS I happen to be using. This gave me a bit of a headache just using $1 as input from the line, as curl only saw the filename as I typed it and didn't have anything for a URL. It wasn't until I echo'd what curl was getting for input that I gleaned what I was doing wrong.

Then I thought, "If there's a $1, shouldn't there be a $2? Single spaces are seen by BASH as a break, if not a delimiter, aren't they?" So I tried this line near the top of the script:
Code:
echo -e "$1 $2"

and invoked the script (with a standby alias, "homecourt"). Sure enough, the URL printed to stdout right after the filename as sure as dinner follows lunch. cUrl downloaded the file, and the code I had written to annotate the dates ran smooth as melted butter.

Here's the final script.
Code:
#!/bin/bash
actiondate=$(/usr/bin/date)
echo -e "$1 $2 $actiondate"
clockdate=$(date '+%d %B, %Y.')
myfile=$1
myurl=$2
curl -o $myfile $myurl
fulldate=$(stat -c %y $myfile)
shortdate=${fulldate%.*}
exiv2 -kM"set Exif.Image.DateTimeOriginal Ascii $shortdate" $myfile
exiv2 -kM"set Iptc.Application2.SpecialInstructions String Downloaded $clockdate" $myfile
exiv2 -kM"set Exif.Image.DocumentName Ascii $myurl" $myfile
echo -ne "File $myfile downloaded and annotated with date and time.\n\n"

Thanks again for the suggestions.

BZT
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change directory within a bash shell script

Hi, I have been trying to execute the below command by changing directory and then copying contents of one directory to another by doing some file name manipulations in between. However this isnt working since as soon as the statement completes it goes back to the original folder. Can someone... (5 Replies)
Discussion started by: HikingLife
5 Replies

2. Shell Programming and Scripting

Is it possible to change paths inside a bash script?

i have some script with some paths inside it. The idea is to some files which is on desktop copy and move to another location. Problem is that inside script is similar to this: cp test1.zip /root/help/ because I allways have another zip files, does it possible to have some input which ask me... (18 Replies)
Discussion started by: tomislav91
18 Replies

3. Shell Programming and Scripting

Windows Task with Bash script

Hello I have a problem with use bash script in windows task. If I use script by cygwin it's working well. If I use it by Windows task I'm get error Error : ERROR 2 (HY000) at line 2: File '.\xxx.csv' not found (Errcode: 2) Code Line : load data local infile './xxx.csv' REPLACE into... (16 Replies)
Discussion started by: karp
16 Replies

4. Shell Programming and Scripting

How to do one line bash schedule task?

This seems to work: https://www.unix.com/shell-programming-scripting/179705-how-run-cygwin-bash-windows-scheduled-task.html However, I was hoping to avoid writing a 2 line bat files to invoke my cygwin scripts as a scheduled task (since I'm making lots scheduled tasks). I was hoping this would... (1 Reply)
Discussion started by: siegfried
1 Replies

5. Shell Programming and Scripting

How to run Cygwin bash from windows scheduled task?

Hmmm.... I love these forums because I always get great prompt responses and I want to ask a question about running bash on windows. Is that allowed? Now I know I can install cygwin cron and run bash that way. Can I run bash from windows schedule task? How? thanks siegfried (1 Reply)
Discussion started by: siegfried
1 Replies

6. Shell Programming and Scripting

Unable to change environment variables in bash script

Hello! For the moment some settings in my .bashrc contain the password of my company's firewall, which is not a good idea. I would like to use the string "PASSWORD" set in .bashrc and a script that changes all appearances of "PASSWORD" in the environment variables by the actual password (which... (4 Replies)
Discussion started by: markolopa
4 Replies

7. Shell Programming and Scripting

Idea for an "informative" bash alias or script

I had the idea come into my head that it would be good to have a single command that gave file type, file size, last modification date, and owner/group information, like what one would see in a GUI "Properties" dialog, but all in a terminal window. In my opinion, these statistics about a file... (5 Replies)
Discussion started by: SilversleevesX
5 Replies

8. UNIX for Dummies Questions & Answers

Alias, function or script (bash) to "revert" cd command?

In all of my brief and superficial experience with Unix or Linux, the one curious and consistent thing has been that 'cd ./' (back up one directory level) has done absolutely nothing in any of them. Now I understand that, at least for bash, 'cd ./' appears to have been substituted by 'cd ..' Am... (1 Reply)
Discussion started by: SilversleevesX
1 Replies

9. UNIX for Dummies Questions & Answers

Query regarding alias and setting bash as a default script

Hi All, I am setting bash as my working shell in my .profile file. So I have written a line : bash as the list line in my .profile I want to use alias as follows: alias me='who am i' When i log in, as expeced I enter the bash shell but alias doesn't work. Is it because the alias is defined... (1 Reply)
Discussion started by: VENC22
1 Replies
Login or Register to Ask a Question