Why am I getting an error when I use `uname` in my .bashrc? - linux

In my .bashrc I have the following code
if [`uname` == "Linux"]; then
echo "It worked"
else
echo "It didn't work"
fi
But when I source my .bashrc I get the following results
[Linux: command not found
It didn't work
Strangly, the [ is not a typo, it is part of the error. If I comment out the if-statement, then the error goes away, so I am pretty sure that it is the source of the error. Plus, if I change the Linux to linux, then the error changes to lowercase also.
And if I echo uname I get Linux.
To source my .bashrc I have used source .bashrc and also have started a new bash session by typing bash on the command line terminal.
I didn't think it was that hard to check for the OS type, but I can't seem to figure out the correct syntax for the .bashrc.
I don't see what I am doing wrong, can anyone help?

You forgot a space after the square brackets. The first line has to look like this:
if [ `uname` == "Linux" ]; then
In your version, without the spaces, the [ and the output of uname is concatenated into one executable named [Linux, which does not exist in the PATH.

Bash in finicky about spacing. You need spaces in your conditional
if [ `uname` == "Linux" ]; then
echo "It worked"
else
echo "It didn't work"
fi

Related

BASH Syntax Checking Debug Mode Malfunction?

We can use bash -n script.sh to validate the syntax of a shell script. However, when I was trying to test this function, I noticed not all the syntax errors could be found by this option.
For example:
root#ubuntu:~/testenv# cat test
#!/bin/bash
SEND=1
if [ "$SEND" -eq 0 ]
echo no
fi
Now, let's test the script:
root#ubuntu:~/testenv# bash -n test
test: line 5: syntax error near unexpected token `fi'
test: line 5: `fi'
It works fine. However, if I just remove one of the bracket:
root#ubuntu:~/testenv# cat test
#!/bin/bash
SEND=1
if [ "$SEND" -eq 0
then
echo no
fi
root#ubuntu:~/testenv# bash -n test
root#ubuntu:~/testenv#
Nothing happened!
I also checked the man page of bash, it describes the "-n" is:
-n Read commands but do not execute them. This may be used to check a
shell script for syntax errors. This is ignored by interactive
shells.
It is a script file, so it shouldn't be an "interactive shell" right? So,how could this happen?
I'm guessing you have run into a very strange quirk of the way the shell implements single-bracketed conditionals: [ is a command, not a special character. Look in your system executable directory (probably /usr/bin) and you will find an executable file literally named [ which implements this command. When you write something like
[ "$SEND" -eq 0 ]
then you're actually invoking the command [ with four arguments:
The value of $SEND
The string -eq
The string 0
The string ]
The command [ checks that the last argument is ] (because it would look weird otherwise), then puts the remaining arguments together to form a condition and return the result of testing the condition.
Now, because [ is a command, it's not a syntax error to invoke that command with any set of arguments you like. Sure, if you leave off the trailing ], you will get an error, but that error comes from the command [, not from the shell. That means you have to actually run the script to get the error - the syntax checker won't see anything wrong with it. As far as bash is concerned, [ is just a command name, no different from, say, my_custom_conditional_test, and if you were to write
my_custom_conditional_test "$SEND" -eq 0
it would be obvious that this is fine, right? Bash thinks of [ the same way.
I should note that for efficiency, bash doesn't actually use the executable file /usr/bin/[; it has its own builtin implementation of [. But people expect [ to act the same way regardless of whether it's built in to the shell or not, so the Bash syntax checker can't give its own [ special treatment. Since it wouldn't be a syntax error to invoke /usr/bin/[ with no trailing ], it can't be a syntax error to invoke the builtin [ without a ].
You can contrast this with [[, which does more or less the same thing (testing a condition) but is given special meaning by the shell. [[ is a special token in shell syntax, not a command. If you write [[ instead of [, and you omit the corresponding trailing ]], you bet Bash is going to complain about a syntax error.

How Can I prevent duplicate change text file in linux?

I have successfully changed text in linux using this shell script
vi -e .bash_profile << END
i
PATH=\$PATH:\$HOME/bin:/usr/pgsql-9.4/bin
export PATH
PGDATA=/usr/pgsql-9.4/data
export PGDATA
.
w
q
END
but I have a problem.
If I execute script twice then duplicate in text file.
So, I want to prevent this situation. How Can I fix it?
Well that depends... if the string "PATH=$PATH:$HOME/bin:/usr/pgsql-9.4/bin" can appear at other places in side the file then we could not use it as an indicator that your vi has already done its work. But if the string otherwise does not appear we could wrap the whole thing
grep -q 'PATH=[$]PATH:[$]HOME/bin:/usr/pgsql-9.4/bin' .bash_profile
if [ $? -eq 1 ]
then
# do your vi stuff here
fi
You might replace the if stuff by
test $? -eq 0 && exit 0
if you were not going to do anything after the vi stuff

Need to fix if else script

I wanted to make a script which should take an argument and check if it is equal to a given word and then display a message accordingly. I use the bash shell of ubuntu OS. I tried something as per the tutorial - http://www.tech-recipes.com/rx/209/bournebash-shell-scripts-string-comparison/ and it failed.
#!/bin/bash
if ["$1"=="password"]
then
echo correct password
else
echo wrong password
fi
bash Script.sh password.
error message is -
[password=password]: command not found.
How to fix it ?
Whitespaces:
#!/bin/bash
if [ "$1" == "password" ]
then
echo correct password
else
echo wrong password
fi
The expression after the if is actually a command, and commands are delimited by whitespaces. So your command is ["$1"=="password"] that expands to [password==password], that obiously does not exist (/usr/bin/[password==password] anyone?).
In my corrected code, the command is [ (yes, there is a /bin/[) and the rest of the line are the arguments.
See man test for details (test is a kind-of-alias for [).

Check $BINDIR value

First of all I need to apology, I have very little Unix/Linux knowledge.
I am following http://dns323.kood.org/howto:subversion to install SVN server on my DLink DNS323 device. I have successfully install Fonz Fun Plug. And I think I installed subversion-1.5.2-1.tgz as well.
According to the document I need to run svnserve.sh, in which part of the soruce code is
svnserve_start() {
if [ -x "{$BINDIR}/svnserve" ]; then
echo "Starting svnserve deamon... "
${BINDIR}/svnserve -d -r ${REPOSITORY}
else
echo "ERROR: svnserve not found or not executable"
fi
}
I got ERROR: svnserve not found or not executable message, so it looks like to me $BINDIR is not defined well.
Anybody knows, how can I do echo to display the value of $BINDIR?
Thanks!
echo $BINDIR before the if statement to check whether {$BINDIR}/svnserve exists.
It seems that {$BINDIR}/svnserve is not executable. Find that file and chmod u+x which add executable attr to it.

Translating Cygwin path to Windows

HI,
I have cygwin installed in my Windows system.
I have written two function in my profile file so that every time I open vi/vim, it will open with gvim.
But with this one of the issue, the windows path and Cygwin path. I tried with Cygpath as below:
function vi ()
{
win_file_path=$(cygpath -w $*)
gvim "$win_file_path" &
}
Bu with this, when ever I open a file like this: "vi /etc/exports +5", it will result in error. So let me know if any of you have any solution.
You can treat the file arguments only:
function vi ()
{
local -a viargs
local a
while [[ $# -gt 0 ]]
do
a="$1"
if [ -e "$a" ]; then a="$(cygpath -w "$a")"; fi
viargs[${#viargs[#]}]="$a"
shift
done
gvim "${viargs[#]}" &
}
Instead of being 'smart' about existing files like this, feel free to simplify to treat just the first argument :)
In recent bash versions you can replace the ugly line
viargs[${#viargs[#]}]="$a"
with
viargs+=( "$a" )
cyg-wrapper has been written for this sole purpose.
NB: See also the related wikia page.

Resources