Bash If Expression Syntax Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About syntax error in conditional expression Us Learn more about Stack Overflow the company Business Learn more about hiring
Syntax Error In Conditional Expression Unexpected Token '
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the
Bash Script If Syntax
Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up bashrc if: Expression Syntax error up
Bash Shell If Syntax
vote 3 down vote favorite I have written the following .bashrc : # .bashrc # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi # User specific aliases and functions function up( ) { LIMIT=$1 P=$PWD for ((i=1; i <= LIMIT; i++)) do P=$P/.. done cd $P export MPWD=$P } function back( ) { LIMIT=$1 P=$MPWD for ((i=1; i <= LIMIT; i++)) do bash while syntax P=${P%/..} done cd $P export MPWD=$P } However, after saving, when I did source .bashrc, i got the following error: if: Expression Syntax. What am I doing wrong ? I googled a while but no avail. bash unix share|improve this question asked Jan 21 '13 at 13:50 Wildling 1,42243162 1 check if your shell is bash using echo $SHELL –JohnTortugo Jan 21 '13 at 13:53 @JohnTortugo Thanks, I was using the c shell and making changes in the bashrc. –Wildling Jan 21 '13 at 14:01 add a comment| 1 Answer 1 active oldest votes up vote 4 down vote accepted if: Expression Syntax is not an error bash would give you. Perhaps your shell is not bash. In fact, as long as if stands alone, any error would not be with if itself: $ if [somethingswrong]; then fail; fi # error, then `[` command must have space around it. -bash: [somethingswrong]: command not found You can check your shell by echoing $SHELL, and you can check which version of bash with $BASH_VERSION. (If the latter is unset, your shell is not bash.) share|improve this answer answered Jan 21 '13 at 13:52 kojiro
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn bash case syntax more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags bash test syntax Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, bash function syntax helping each other. Join them; it only takes a minute: Sign up syntax error for if statement in shell script up vote 0 down vote favorite I have been getting this error for about an hour now. It http://stackoverflow.com/questions/14440105/bashrc-if-expression-syntax-error says: if: Expression Syntax when I try to run my shell script. I could not find the problem in the code so I shortened everything down to this simple conditional: if [ $# -eq 0 ] then echo "ERROR - no arguments given" exit 1 fi I have looked all over the internet and this syntax appears to be flawless.. So why do I still get the if expression syntax error??? shell share|improve this question asked http://stackoverflow.com/questions/13673520/syntax-error-for-if-statement-in-shell-script Dec 2 '12 at 20:29 Matt Hintzke 2,43932458 1 What's your shell? –Jim Stewart Dec 2 '12 at 20:46 im not sure. i am using a terminal that my school manages. is there a command to find it? –Matt Hintzke Dec 2 '12 at 21:35 add a comment| 1 Answer 1 active oldest votes up vote 1 down vote accepted I think you are using csh/tcsh, so you have to write if ( $# == 0 ) then echo "ERROR - no arguments given" exit 1 endif (With bash your script runs ok here) share|improve this answer answered Dec 2 '12 at 20:37 pbhd 3,055720 hmmm idk how you knew that but thanks a lot! Actually the first part is right but instead of endif, i still needed fi. No idea why. My professor taught us the wrong syntax to everything –Matt Hintzke Dec 2 '12 at 20:42 He probably taught you sh and your error is that you use a different shell. They are similar, but still, it's sort of like complaining that the Prolog compiler won't accept C. –tripleee Dec 2 '12 at 20:52 1 @Matt guessed it because it works perfect with bash and the error-msg looks like csh. But that fi your shell wants makes me curious: which system and shell are you using exactly?
an if … then construct or a while loop. The syntax of these conditions can seem a bit daunting to learn and use. This tutorial aims to https://linuxacademy.com/blog/linux/conditions-in-bash-scripting-if-statements/ help the reader understanding conditions in bash, and provides a comprehensive list of the possibilities. A small amount of general shell knowledge is assumed. Difficulty: Basic - Medium Introduction Bash features a http://www.linuxquestions.org/questions/programming-9/if-expression-syntax-error-in-csh-if-statement-4175412232/ lot of built-in checks and comparisons, coming in quite handy in many situations. You've probably seen if statements like the following before: if [ $foo -ge 3 ]; then The condition syntax error in this example is essentially a command. It may sound strange, but surrounding a comparison with square brackets is the same as using the built-in test command, like this: if test $foo -ge 3; then If $foo is Greater then or Equal to 3, the block after ‘then' will be executed. If you always wondered why bash tends to use -ge or -eq instead syntax error in of >= or ==, it's because this condition type originates from a command, where -ge and -eq are options. And that's what if does essentially, checking the exit status of a command. I'll explain that in more detail further in the tutorial. There also are built-in checks that are more specific to shells. What about this one? if [ -f regularfile ]; then The above condition is true if the file ‘regularfile' exists and is a regular file. A regular file means that it's not a block or character device, or a directory. This way, you can make sure a usable file exists before doing something with it. You can even check if a file is readable! if [ -r readablefile]; then The above condition is true if the file ‘readablefile' exists and is readable. Easy, isn't it? The syntax of an if statement (a short explanation) The basic syntax of an if … then statement is like this: if
Search HCL Search Reviews Search ISOs Go to Page... LinuxQuestions.org > Forums > Non-*NIX Forums > Programming [SOLVED] if: Expression syntax error in csh if statement User Name Remember Me? Password Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game. Notices Welcome to LinuxQuestions.org, a friendly and active Linux Community. You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today! Note that registered members see fewer ads, and ContentLink is completely disabled once you log in. Are you new to LinuxQuestions.org? Visit the following links: Site Howto | Site FAQ | Sitemap | Register Now If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here. Having a problem logging in? Please visit this page to clear all LQ-related cookies. Introduction to Linux - A Hands on Guide This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter. For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own. Click Here to receive this Complete Guide absolutely free. Search this Thread 06-19-2012, 06:07 AM #1 Mark_667 Member Registered: Aug 2005 Location: Manchester, England Distribution: Ubuntu 14.10 Posts: 293 Rep: if: Expression syntax error in csh if statement I've got a simple csh script that checks for the exit code of grep and echos the result: Code: #!/bin/csh if (df -k | /usr/xpg4/bin/grep -q /mnt/Backup == 0) then echo 'Mount worked' else echo 'Mount failed' endif Unfortunately I keep getting: if: Expression syntax Can anyone se