Catching empty variable in touch alias

L Carver

New Member
Joined
Sep 29, 2018
Messages
4
Reaction score
2
Credits
0
I have a bash alias called ttouch. It's a shortcut to touch -t, which takes a variable (tdate) and a file on which to apply it, represented (most often but not always) by a variable such as $picc.

I'm trying to write a function so when the user (always me) forgets to supply the target, touch doesn't return an error to stdout. But I don't know quite how to set the second value in an if...then statement.

Thanks in advance,

Carver
 


Would something like this suit your needs?
Code:
function ttouch()
{
  if [[ $# -ne 2 ]]; then
    echo "ttouch requires two parameters - date-format and file-name"
    return 1
  fi
  touch -t "$1" "$2"
  return $?
}

Which you would use in the terminal like this:
Code:
ttouch 01041337 filename.
The above usage would create a file called "filename" dated 1st of April of the current year at 13:37hrs. Or if the file already exists, it's timestamp will be set to April 1st @ 13:37hrs.

Or you could call it from another bash script, where the date and file-name are stored in variables:
Code:
ttouch $tdate $picc

The if statement in the ttouch function checks that ttouch received exactly two parameters. If we don't have exactly 2 parameters, the function returns with an error code (1).

Butif we do have two parameters, we use them in the 'touch -t' command.

NOTE: The final return value from the ttouch function will be the return code from the 'touch -t' command ($?).

Which may or may not be enough for you.

If that's not quite what you are looking for, show me the code you have so far and I'll see what I can do!

[EDIT]
BTW: I know you said you didn't want touch to output error messages to the screen. And my little function does echo an error message when less than two parameters are passed to the function. That error message is only there to illustrate what is going on in the code. If you don't want the error message going to the screen, you could either completely remove it, or redirect it to a log-file.
[/EDIT]
 
Last edited:
How about redirecting the error to the "virtual ignore zone," ie 2 >/dev/null.
Or has that been deprecated in the progression from BASH 4.0.0 to whatever it is now on the Debian repos?

Carver
 
Which error message are you talking about redirecting?
The error message I added to my function? or the one thrown by touch when you run your script?

If you're talking about the echo statement in my script - surely it would make more sense to simply remove it if it is not needed. Because there is no point echoing something directly to the bit-bucket! May as well just do nothing!
But you could append 2&> /dev/null to the end of the touch command used in my function to hide all output from touch....
e.g.
Code:
touch -t $1 $2 2&> /dev/null

As for changes to bash - I have no idea! I haven't looked at any of the changes to any of the recent versions.
Debian testing currently has version 4.4.23. But I don't think there is any chance that the bit bucket has been deprecated!
 

Members online


Top