Variable syntax error in $?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable syntax error in $?
# 1  
Old 05-26-2013
Variable syntax error in $?

hi all ,

i just tried to take the status of previous command inside the script using
echo $?. It throws me a variable syntax error , but when i use echo $? as an individual command it works perfectly .

can anyone Please tell me why am getting a variable syntax error when i use echo $? inside a script . am using csh shell ..

thanks a lot in advance ..
# 2  
Old 05-26-2013
Please post your exact code, not what you interpret it to be.
# 3  
Old 05-26-2013
Variable syntax error

Code:
Code:
#!/bin/csh -f

################
# Set Variables
################

set curdate = `date '+%Y%m%d'`
set datetime = `date '+%m%d%y_%H%M'`

grep abc abc.txt

echo $?

exit(0)

Ideally this should return status 2 since there is no file called as abc.txt in my directory.. but it throws a variable syntax error

---------- Post updated at 11:31 AM ---------- Previous update was at 11:28 AM ----------

my actual need is I should have a script which should call a script 1.sc and when it is success it should call 2.sc and when then so on .. the 2nd script should not run unless 1.sh is completed.
I tried using ps -ef | grep scriptname | awk '{print $2}' , but it always returns some PID . so i tried using echo $? .. please corrct me if my logic is wrong ..

Last edited by Franklin52; 05-27-2013 at 03:09 AM.. Reason: Code tags
# 4  
Old 05-26-2013
Hi.

In the real csh, not tcsh, you probably need to use $status, not $?

This is just one of the many reasons that most people use and recommend Bourne shells (sh, ksh, bash, zsh) for scripting.

Best wishes ... cheers, drl

Last edited by drl; 05-26-2013 at 01:39 PM..
# 5  
Old 05-26-2013
thanks a lot drl .. it worked great .. you have any comments about my logic ??
can u pls throw me some light over that if possible
# 6  
Old 05-26-2013
Hi,

To be sure to avoid running Script #2 before the #1 is done, you can try using lock file.
You may also want to chain your scripts with a success condition, would look like (in bash) :

Code:
script1.bash && script2.bash && script3.bash

A a side thought my first advice would be : avoid using csh, there are quite some good reasons no to use it , but sometimes, one do not have the choice....
# 7  
Old 05-26-2013
Code:
script1.bash && script2.bash && script3.bash

is short for
Code:
script1.bash
if ($status == 0) script2.bash
if ($status == 0) script3.bash

in csh.
Or in sh
Code:
#var=value
script1.bash
[ $? = 0 ] && script2.bash
[ $? = 0 ] && script3.bash

The shell by default waits until a command exits.
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash function using variable in it syntax error

The below bash function uses multiple variables CODING, SAMPLE, SURVEY, andvariant in it. The user selects the cap function and details are displayed on the screen using the $SURVEY variable, the directory is changed to $SAMPLE and the samples.txt is opened so the user can select the sample to... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

"Syntax Error sometimes due to corruption of variable value "

Hi , I have script as follows , #!/usr/bin/ksh -x if then alias echo="echo -e" fi MAX_ENTRIES=1024 nb_of_entries=`echo "$list_of_entries" | wc -w` # Set number of tables eval nb_of_tables=\`expr `expr $nb_of_entries / $MAX_ENTRIES` + 1 \` # Output the number of tables echo... (6 Replies)
Discussion started by: breezevinay
6 Replies

3. Shell Programming and Scripting

Syntax error: Bad for loop variable

I'm getting an error while running this script. Need help. set -x verbose #echo on clear #clear the screen USERNAME="bbb" PASSWORD="password" SERVER="192.168.1.100" WAIT_TIME=300 FILE_PATH="/home/users/xxx/MMM" # local directory to pickup *.dat file REMOTE_PATH="/Drop_off/xxx/yyy" #... (17 Replies)
Discussion started by: clgz2002
17 Replies

4. Shell Programming and Scripting

Syntax error: Bad for loop variable

Hi Can any one help, I'm trying to run a script that beeps out the ip address from the PC internal speaker with the following script. It keeps throwing the error "Syntax error: Bad for loop variable" on line 16. I know its picking up the IP ADDRESS correctly. Any ideas on whats wrong. I'm... (3 Replies)
Discussion started by: dman
3 Replies

5. Shell Programming and Scripting

Syntax error on variable assignment

Hello all, I have "inherited" a Korn shell script I'm supposed to maintain, and running a "sh -n" on it, I got this syntax error: script.sh: syntax error at line 63: `OB_DEVICE=$' unexpected The line in cause is the first occurence of the usage of perl one-liners. The whole line: ... (2 Replies)
Discussion started by: AdrianM
2 Replies

6. Shell Programming and Scripting

How do I write multiple variable syntax?

I am trying to write a script that will do a sql statement. But in the sql I will have a list (1,2,3,4). How would I go about asking for the input from the user running the script, and then transferring that back into the list as say ($var1,$var2,$var3)? It would be somewhat like this: ... (1 Reply)
Discussion started by: Captain
1 Replies

7. UNIX for Dummies Questions & Answers

Variable syntax and lines of code

I am trying to use use the AWK and EGREP commands together in an AWK script. My overall objective is to count lines of code but exclude comments and blank lines and such. I am able to use AWK with the FIND command like so: | awk '{print \"wc -l \"$1}' > lineCount.sh Now when I modify the... (7 Replies)
Discussion started by: mastachef
7 Replies

8. Solaris

C-shell: variable syntax question

There is a possibility to set a variable, having an another variable in it's name: prompt% setenv PRT one prompt% setenv VAR_${PRT} value prompt% So, this way the VAR_one = "value" and could be viewed: prompt% echo VAR_one value prompt% Q: How to view a variable having another... (0 Replies)
Discussion started by: alex_5161
0 Replies

9. UNIX for Dummies Questions & Answers

Setting a variable (need syntax help)

I need some syntax help (working in a bash shell) I have a variable which is a filename with an extension, and I need to create another variable with the same name but a different extension To explain, the input file should be called something like "filename.L1" and the output file should be... (1 Reply)
Discussion started by: Slanter
1 Replies

10. Shell Programming and Scripting

Syntax to export any variable

How to export variables on a UNIX prompt. Please provide me syntax. Thanks in advance. Malay (5 Replies)
Discussion started by: malaymaru
5 Replies
Login or Register to Ask a Question