Automate "touch" bash script not working.


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Automate "touch" bash script not working.
# 1  
Old 05-30-2017
Question Automate "touch" bash script not working.

Code:
#!/bin/bash -i
SAVEIFS=$IFS
IFS=$"\n\b"

picc=$*
if [ -f "$picc" ]; then
echo $TDATE
if [ ! -z "$TDATE" ]; then
 touch dummy
 touch -t "tdate" dummy
 touch -r "dummy" "$picc"
else
    echo -e "No mod date value to apply. If there is one in your shell,\ninvoke \e[7msuperdate\e[0m and try again."
    exit 1
fi
fi
IFS=$SAVEIFS

Started asking advice on this (on Linuxquestions.org). The last time I ran the script, this happened.
the script, when I run it, is neither changing the date of the file pointed to nor creating the file "dummy." And the command in line 15 the script, when I run it, is neither changing the date of the file pointed to nor creating the file "dummy." And the command in line 15 isn't even returning an "empty variable" kind of error. In short, the script does nothing I can discern between running it and not running it.

Anyone care to advise me?

Carver
Go here: Automate "touch" bash script not working. for the original discussion
# 2  
Old 05-30-2017
Quote:
Originally Posted by iamwrong
Anyone care to advise me?
Start with the standard explanations:

- what OS are you using?
- what shell are you using?

- please describe (in plain text) what you are about to achieve.

From the quoted and linked discussion i take it you don't understand what export variable does and you don't understand how to create and use variables and what their scope is (that is: where they keep their values and where not). From your script i take it you don't know how to handle command line options either.

I will be glad to explain all that (and maybe more) to you once you have told us what the (real!) underlying motivation for your script is because chances are you need some other explanation(s) either in addition or instead of knowing how to handle variables, command line arguments, etc..

I hope this helps.

bakunin
# 3  
Old 05-30-2017
I said bash in the subject line

GNU bash, version 4.3.48(1)-release (i686-pc-linux-gnu)
LinuxMint 18.3 KDE 5.5.x

I wish to export the value of a variable (a date in MMDDYYHHMM.SS format; in the script: tdate or TDATE) set in a terminal shell to a script that will change the modification date of a file (referred to by the value of another variable, here picc) without my having to type in the relevant command. I have my laptop on an ordinary desk (in fact it has an Ethan Allen stamp on the inside of the drawer). My laptop has a 17" screen, and on the only chair I have for the desk, the armrests are too high for me to slide them under it. This makes it difficult for me to reach the laptop's keyboard, so I am using an ordinary desktop keyboard, set at an angle with the bottom resting on a ridge inside the front of the drawer. Therefore, I have to type "uphill," as I call it, an unconventional ngle in my experience, for typing. And since my typing is not the sort as taught in high school or business schools, I try to get away with doing as little of it as possible.

But enough of all that. I went about that explanation so you would know that, besides a script that isn't doing what I'd like, I have other challenges to contend with. No one on linuxquestions knows about them

It was indeed unfair of me not to put my OS, shell version and hardware in my signature on this forum. I will do so as soon as I finish this post.

wrong
This User Gave Thanks to iamwrong For This Post:
# 4  
Old 05-30-2017
You are first using variable TDATE without setting it then trying to use tdate (without a $ either) to set the timestamp of your file so could these be issues?


What are you trying to actually achieve?

If you stick to one variable name, perhaps this will help:-
Code:
TDATE=201612311234
touch -t $TDATE /tmp/dummy

This should set the file /tmp/dummy to have timestamp of 12:34 on the last day of 2016.

Does that help?


Robin
# 5  
Old 05-30-2017
That you are typing at an angle isn't relevant. The computer doesn't know or care.

TDATE is not set. That is why it is not working. You have to set it or inherit it from somewhere. It won't just appear because you want it to.

I must assume you are setting it somewhere else, or attempting to set it from somewhere else. This is the bit you haven't explained. You also haven't explained how you are running this program - manually, from cron, whatever.
# 6  
Old 05-30-2017
First,
Code:
#!/bin/bash -i

tells the interpreter that this is an interactive shell and it reads your .bashrc file before executing the rest of your script. Is this what you want?
Secondly consider this:
Code:
#!/bin/bash 
IFS=$"\n\b"

picc=$*

printf "%s\n" $picc

Let's call it picc:
Code:
$ ./picc

$ ./picc eeny
ee
y
$ ./picc eeny meeny
ee
y
mee
y

You have changed $IFS to be "nb" not "\n\b".
Change the line
Code:
IFS=$"\n\b"

to
Code:
IFS=$'\n\b'

and try again:
Code:
$ ./picc

$ ./picc eeny
eeny
$ ./picc eeny meeny
eeny
meeny

Somehow I don't think you are expecting either of those behaviours for $picc. It looks to me as though you are expecting one word. Do your filenames have spaces in them?

When you get that right maybe the rest will slot into place. Until you do what happens with $tdate won't matter.

Andrew
These 2 Users Gave Thanks to apmcd47 For This Post:
# 7  
Old 06-01-2017
Dear iamwrong,
I hope we haven't driven you to another forum already. After considering you script I've come up with this albeit over-engineered script which may have the behaviour I think you wanted. I have included comments to explain why I ave done the things I have.
Code:
#!/bin/bash         
                    # no need for the -i option
                    # IFS statements removed as you don't actually need them

usage() {           # Few do it but you really should have a usage
                    # function even on a script this short
   printf "Usage: %s file\n" ${0##*/}
   printf "Where file is the file to be timestamped.\n"
   printf "Remember to quote the filename if there are spaces in it.\n"
   printf "Example: %s %s\n" ${0##*/} $'"my holiday snap.jpg"'
   printf "Required environment variable: TDATE\n"
   printf "Please ensure it is exported with %s\n" $'\e[7msuperdate\e[0m'
}

if [ -z "${TDATE}" ]    # Do this test first
then                    # exit if it fails
   printf "\nTDATE not set\n\n"
   usage
   exit 1
fi

ref_file=/tmp/${0##*/}$$        # set up your reference file
trap 'rm -f "${ref_file}"' 0    # remove the reference file on exit
                                # The -f silences rm if file hasn't been created
                                # when we exit
trap 'exit' HUP INT QUIT ABRT TERM  # clean exit on any of these interrupts
                                    # Probably not useful in this short script

touch -t "${TDATE}" "${ref_file}"

picc="${1}"
if [ -z "${picc}" ] || [ ! -f "${picc}" ]
then    # can't find $picc
   printf "\nCannot find file to update!\n\n"
   usage
   exit 1
fi

touch -r "${ref_file}" "${picc}"       # business of this script 

# exit trap clears the reference file

I am guessing that the reason you used
Code:
picc=$*

in your original code was to allow for spaces in your file names. If so this script is more likely to work the way you wanted it to.

I hope you find this useful. As I said it is somewhat over-engineered, what with using a reference file for the touch command and the resultant interrupt handling, as well as the usage function.

Andrew
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

"if" Loop not working when executing script using cron

I am facing this weird issue where the script is working fine from the command line but when I am executing it from cron though it is working fine but the "if" loop is processing else part though I know that the if part of the logic is true and ideally the loop should execute the if portion. ... (3 Replies)
Discussion started by: sk2code
3 Replies

3. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

4. Shell Programming and Scripting

"find . -printf" without prepended "." path? Getting path to current working directory?

If I enter (simplified): find . -printf "%p\n" then all files in the output are prepended by a "." like ./local/share/test23.log How can achieve that a.) the leading "./" is omitted and/or b.) the full path to the current directory is inserted (enclosed by brackets and a blank)... (1 Reply)
Discussion started by: pstein
1 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

7. Shell Programming and Scripting

"sed" command is not working in shell script

Hi All, I am not much strong in shell scripting... I am using sed command in my script to find and replace a string....... This is how script looks : ############# #!/usr/bin/ksh CONFIG_FILE=iom_test.txt FIND=`echo "NIS_FTP_SERVER1=123.456.iom.com"` REPLACE=`echo... (2 Replies)
Discussion started by: askumarece
2 Replies

8. Shell Programming and Scripting

Script not working..."sort" not working properly....

Hello all, I have a file - 12.txt cat 12.txt =============================================== Number of executions = 2 Total execution time (sec.ms) = 0.009883 Number of executions = 8 Total execution time (sec.ms) = 0.001270 Number of... (23 Replies)
Discussion started by: Rahulpict
23 Replies

9. Shell Programming and Scripting

Can I make "touch" create executable files by manipulating umask?

I'm getting to grips with this concept of the umask. What I thought was, setting umask uga+rwx would result in creating files with all permissions for everyone. Seems not to be the case though. Read and write bits get set, but not the execute bit. Is there some gap in my understanding, or is... (2 Replies)
Discussion started by: tphyahoo
2 Replies

10. Shell Programming and Scripting

Why generate "ash and bash" different output for same bash script?

Hi, For my bash script, terminal with bash is generate an OK output and program works right. already, terminal with ash have "line 48: syntax error: Bad substitution" output and program don't work. :confused: (0 Replies)
Discussion started by: s. murat
0 Replies
Login or Register to Ask a Question