Issue with variables via cronned script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Issue with variables via cronned script
# 1  
Old 10-18-2010
Issue with variables via cronned script

I have script sourcing the profile of functional user like as below

Quote:
. ${HOME}/.profile
and my .profile for functional user has below entry
Quote:
ttyname=$(/usr/bin/tty)
now when i cron the script i am receiving the below error after execution, though i could see the output of variable when i do echo as below.
Quote:
echo $ttyname
/dev/pts/17
can you tell me where could be the issue, The error messase is:
syntax error at line 37: `ttyname=$' unexpected
# 2  
Old 10-18-2010
Would the script launch by cron need that tty ?

Maybe use the excellent tool GNU screen (FTP)

maybe try backtick quote `` instead of $()

Last edited by ctsgnb; 10-18-2010 at 09:12 PM..
# 3  
Old 10-18-2010
Please state the Operation System and version and the Shell you are using.
Please post the complete script, obliterating anything confidential.

If you are confident with cron, please post the output from the unix "env" command when run from cron.

I strongly suspect that the default shell for your cron is something like a Bourne Shell which does not understand "$( )" notation.
# 4  
Old 10-19-2010
Operating System Details:
Quote:
uname -a
SunOS xyz-vh02 5.10 Generic_141414-07 sun4v sparc SUNW,T5240

echo $SHELL
/bin/ksh
It has nothing to do with the script its just sourcing the profile of functional user and when the script executes via crontab it throwing out the below error.
Quote:
syntax error at line 37: `ttyname=$' unexpected
# 5  
Old 10-19-2010
Remember that a ksh script lacking either a correct first line #!abs_path_ksh or execute permissions is sent to sh as stdin, see man exec(2).

---------- Post updated at 10:16 AM ---------- Previous update was at 10:05 AM ----------

Also, crontab commands are sent first to sh, after cron metacharacter expansion. I like good logging to dated log files, not to root or email, but 'cron' likes the '%' more than 'date'! I wrote a ksh script 'cron_date' that swapped the ~ with %, so I could write cron lines like this one with daily logs:

Code:
25 * * * * my_command >>/var/tmp/my_command_log.`/usr/local/bin/cron_date '+~Y~m~d'` 2>&1

# 6  
Old 10-19-2010
I think of something else about your issue:
You could test $TERM to see if it had been set. If you are running
something from cron, it would not have a value.

So the solution is to test if interactive:
Code:
case $- in
   *i*)  echo interactive ;;
     *)  echo not interactive ;;
esac

test to be put in your .profile...
# 7  
Old 10-19-2010
It helps if you put all the terminal stuff in one area of your .profile and use something (I ike ttyname=$( tty 2>&1 ) != "not a tty" ) to keep out scripts with no tty.

Similarly, .profile can have ksh options in a section where you ensure the current shell is ksh not sh.

It beats maintaining multiple personal environment scripts.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue nesting variables in csh.

The variables given are already defined ($file1-$file3, $obsid1-$obsid3, and $n=3). When I go to run the code, the terminal outputs "Missing }." I believe the error is with the nesting of variables. It would save a lot of time getting this while loop working. set i = 1 while (${i} <=... (5 Replies)
Discussion started by: ojdefdidit
5 Replies

2. Solaris

Solaris 8 ssh issue - $SSH_ORIGINAL_COMMAND undefined variables

I face a weird question I don't know how to deal with. I tried to limit the permission of root user to remote login using ssh. So I did the following for a client server, 1. edit /usr/local/etc/sshd_config and modify as below PermitRootLogin forced-commands-only 2. using pubkey... (7 Replies)
Discussion started by: bestard
7 Replies

3. Shell Programming and Scripting

Issue in shell script variables

Hi, I have a file at $HOME/t.txt, below is file content cat $HOME/t.txt CUSTOMER_${REGION}.out Am using this file content in one script $HOME/samp.sh, below is the script #!/bin/bash REGION=USA x=`cat ${HOME}/t.txt` echo $x Am getting following output.. CUSTOMER_${REGION}.out ... (3 Replies)
Discussion started by: shieksir
3 Replies

4. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

5. Shell Programming and Scripting

Scripting Issue with Variables from awk

Greetings all, Disclaimer: I'm a novice and always welcome best practices as I'm learning. File example: 100,1.1.1.1,1.1.1.2,10.10.10.1,172.16.1.10,172.16.1.20 101,1.1.2.1,1.1.2.2,10.10.20.1,172.16.2.10,172.16.2.20 102,1.1.3.1,1.1.3.2,10.10.30.1,172.16.3.10,172.16.3.20 ...and so on ... (3 Replies)
Discussion started by: sjrupp
3 Replies

6. Shell Programming and Scripting

awk issue expanding variables in ksh script

Hi Guys, I have an issue with awk and variables. I have trawled the internet and forums but can't seem to get the exactt syntax I need. I have tried using awk -v and all sorts of variations but I have hit a brick wall. I have spent a full day on this and am just going round in circles. ... (3 Replies)
Discussion started by: gazza-o
3 Replies

7. Shell Programming and Scripting

Awk script problem - Variables Causing Issue

can someone please explain to me what i'm doing wrong with this code: WELT=$(awk '(($1 ~ "^${caag}$") || ($2 ~ "^${caag}$"))' /tmp/Compare.TEXT) when run from the command line, it works. but it seems to be having a problem doing the comparison when variables are involved. i tested from... (1 Reply)
Discussion started by: SkySmart
1 Replies

8. Shell Programming and Scripting

White spaces issue with shell variables

Hi all, I've a requirement as below Source file src.txt sample data: A<10 white spaces>B12<5 white spaces>C<17 white spaces> A1<5 white spaces>B22<5 white spaces>C13<17 white spaces> when I'm fetching a record from this file into a shell variable like below: vRec=`head -1 src.txt... (2 Replies)
Discussion started by: madhu_1126
2 Replies

9. UNIX for Dummies Questions & Answers

testing variables issue

oh,i don't understand my shell file like this #!/bin/sh # # testnum=$(ifconfig eth0|sed -n 8p|awk '{print $2}'|cut -c7-) echo $testnum sleep 2 echo $testnum sleep 2 echo $testnum sleep 2 echo $testnum but the output was always the same value like # ./test.sh 3779052732 3779052732... (8 Replies)
Discussion started by: flw521521
8 Replies

10. UNIX for Dummies Questions & Answers

Issue with parsing config variables

I am using MKS tool kit on windows server. One config variable is defined in windows environment and I am trying to use that variable. # Below RootDir is defined in windows RootDir="\\f01\var" # in unix script details="$RootDir/src|$RootDir/tgt" src=`echo $details|awk -F '|' '{print... (1 Reply)
Discussion started by: madhukalyan
1 Replies
Login or Register to Ask a Question