Thread: [Linuxcommand-discuss] question about redirection
Brought to you by:
bshotts
From: Bram M. <bra...@li...> - 2004-02-11 14:37:16
|
Hi I would like to use the following script to start evolution from now on so the output would be logged. Can I replace the "/data/logs/evolution.log" with a variable? Right now it looks quite ugly and reusing the script for other apps would involve too much typing/a high risk of typos/not enough laziness :) --- script --- #!/bin/bash echo "" >> /data/logs/evolution.log echo "--- $(date +"%R %A, %B %-d, %Y") ---" >> /data/logs/evolution.log evolution >> /data/logs/evolution.log 2>&1 --- end --- Can I replace it with something like: --- script --- #!/bin/bash logfile="/data/logs/evolution.log" echo "" >> $logfile echo "--- $(date +"%R %A, %B %-d, %Y") ---" >> $logfile evolution >> $logfile 2>&1 --- end --- TIA -- # Mertens Bram "M8ram" <bra...@li...> Linux User #249103 # # SuSE 8.2 Pro kernel 2.4.20-4GB i686 128MB RAM # # 3:30pm up 19 days 19:08, 6 users, load average: 0.37, 0.27, 0.12 # |
From: William S. <bs...@pa...> - 2004-02-11 14:51:42
|
Yes, by all means you would want to use a variable instead. It would make your script easier to maintain. I would suggest that in keeping with convention, the name of the variable should have an uppercase name since it you are treating the variable as a constant. ||||| William Shotts, Jr. (bshotts AT panix DOT com) ||||| Be a Linux Commander! Follow me to http://linuxcommand.org On Wed, 11 Feb 2004, Bram Mertens wrote: > Hi > > I would like to use the following script to start evolution from now on > so the output would be logged. > > Can I replace the "/data/logs/evolution.log" with a variable? Right now > it looks quite ugly and reusing the script for other apps would involve > too much typing/a high risk of typos/not enough laziness :) > > --- script --- > #!/bin/bash > > echo "" >> /data/logs/evolution.log > echo "--- $(date +"%R %A, %B %-d, %Y") ---" >> /data/logs/evolution.log > evolution >> /data/logs/evolution.log 2>&1 > --- end --- > Can I replace it with something like: > --- script --- > #!/bin/bash > > logfile="/data/logs/evolution.log" > > echo "" >> $logfile > echo "--- $(date +"%R %A, %B %-d, %Y") ---" >> $logfile > evolution >> $logfile 2>&1 > --- end --- |
From: Bram M. <bra...@li...> - 2004-02-11 16:11:00
|
On Wed, 2004-02-11 at 15:51, William Shotts wrote: > Yes, by all means you would want to use a variable instead. It would make > your script easier to maintain. I would suggest that in keeping with > convention, the name of the variable should have an uppercase name since > it you are treating the variable as a constant. After editing the script I get the following errors: m8ram@linux:~> bin/startevo bin/startevo: line 3: =/data/logs/evolution.log: No such file or directory bin/startevo: line 4: $LOGFILE: ambiguous redirect bin/startevo: line 5: $LOGFILE: ambiguous redirect bin/startevo: line 6: $LOGFILE: ambiguous redirect This is what the script looks like now: m8ram@linux:~> cat bin/startevo #!/bin/bash $LOGFILE="/data/logs/evolution.log" echo "" >> $LOGFILE echo "--- $(date +"%R %A, %B %-d, %Y") ---" >> $LOGFILE evolution >> $LOGFILE 2>&1 The file does exist and is writable: m8ram@linux:~> ls -lh /data/logs/evolution.log -rw-r--r-- 1 m8ram users 50K 2004-02-11 16:58 /data/logs/evolution.log Why is bash looking for a file called '=/data/logs/evolution.log' ? The quotes are correct, aren't they? TIA -- # Mertens Bram "M8ram" <bra...@li...> Linux User #249103 # # SuSE 8.2 Pro kernel 2.4.20-4GB i686 128MB RAM # # 5:06pm up 19 days 20:43, 5 users, load average: 0.56, 0.20, 0.10 # |
From: William S. <bs...@pa...> - 2004-02-11 17:57:31
|
I see your problem. See my comment below... ||||| William Shotts, Jr. (bshotts AT panix DOT com) ||||| Be a Linux Commander! Follow me to http://linuxcommand.org On Wed, 11 Feb 2004, Bram Mertens wrote: > On Wed, 2004-02-11 at 15:51, William Shotts wrote: > > Yes, by all means you would want to use a variable instead. It would make > > your script easier to maintain. I would suggest that in keeping with > > convention, the name of the variable should have an uppercase name since > > it you are treating the variable as a constant. > > After editing the script I get the following errors: > m8ram@linux:~> bin/startevo > bin/startevo: line 3: =/data/logs/evolution.log: No such file or directory > bin/startevo: line 4: $LOGFILE: ambiguous redirect > bin/startevo: line 5: $LOGFILE: ambiguous redirect > bin/startevo: line 6: $LOGFILE: ambiguous redirect > > This is what the script looks like now: > m8ram@linux:~> cat bin/startevo > #!/bin/bash > > $LOGFILE="/data/logs/evolution.log" This is wrong. It should be: LOGFILE="/data/logs/evolution.log" No leading "$". $ tells bash to perform a substition. It does not indicate a variable type as it would in say VisualBasic. Also, I assume that /data/logs exists and you have permission to read/write a file in that directory. > echo "" >> $LOGFILE > echo "--- $(date +"%R %A, %B %-d, %Y") ---" >> $LOGFILE > evolution >> $LOGFILE 2>&1 > > The file does exist and is writable: > m8ram@linux:~> ls -lh /data/logs/evolution.log > -rw-r--r-- 1 m8ram users 50K 2004-02-11 16:58 /data/logs/evolution.log > > Why is bash looking for a file called '=/data/logs/evolution.log' ? The > quotes are correct, aren't they? > > TIA > -- > # Mertens Bram "M8ram" <bra...@li...> Linux User #249103 # > # SuSE 8.2 Pro kernel 2.4.20-4GB i686 128MB RAM # > # 5:06pm up 19 days 20:43, 5 users, load average: 0.56, 0.20, 0.10 # > > > > ------------------------------------------------------- > SF.Net is sponsored by: Speed Start Your Linux Apps Now. > Build and deploy apps & Web services for Linux with > a free DVD software kit from IBM. Click Now! > http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click > _______________________________________________ > Linuxcommand-discuss mailing list > Lin...@li... > https://lists.sourceforge.net/lists/listinfo/linuxcommand-discuss > |
From: Bram M. <bra...@li...> - 2004-02-11 22:27:08
|
On Wed, 2004-02-11 at 18:56, William Shotts wrote: > I see your problem. See my comment below... [...] > > $LOGFILE="/data/logs/evolution.log" > > This is wrong. > It should be: > > LOGFILE="/data/logs/evolution.log" > > No leading "$". $ tells bash to perform a substition. It does not > indicate a variable type as it would in say VisualBasic. Thanks that did it indeed, I also changed: echo "" >> $LOGFILE echo "--- $(date +"%R %A, %B %-d, %Y") ---" >> $LOGFILE to: echo "\n--- $(date +"%R %A, %B %-d, %Y") ---" >> $LOGFILE Just to clean things up. Regards Bram -- # Mertens Bram "M8ram" <bra...@li...> Linux User #249103 # # SuSE 8.2 Pro kernel 2.4.20-4GB i686 128MB RAM # # 11:26pm up 20 days 3:04, 5 users, load average: 0.01, 0.05, 0.01 # |
From: Bram M. <bra...@li...> - 2004-02-11 22:34:26
|
On Wed, 2004-02-11 at 23:26, Bram Mertens wrote: [...] > Thanks that did it indeed, I also changed: > echo "" >> $LOGFILE > echo "--- $(date +"%R %A, %B %-d, %Y") ---" >> $LOGFILE > to: > echo "\n--- $(date +"%R %A, %B %-d, %Y") ---" >> $LOGFILE Hmm that didn't work as expected either, after looking at `info echo` I realised it had to be: echo -e "\n--- $(date +"%R %A, %B %-d, %Y") ---" >> $LOGFILE Regards -- # Mertens Bram "M8ram" <bra...@li...> Linux User #249103 # # SuSE 8.2 Pro kernel 2.4.20-4GB i686 128MB RAM # # 11:32pm up 20 days 3:10, 5 users, load average: 0.44, 0.19, 0.08 # |
From: William S. <bs...@pa...> - 2004-02-11 22:47:42
|
You got it. ||||| William Shotts, Jr. (bshotts AT panix DOT com) ||||| Be a Linux Commander! Follow me to http://linuxcommand.org |