undefined variable error in csh script - linux

I have one function in csh script and in this function I am using one variable which is sourced from one file. But while using script its throwing undefined error for same variable.
I am using Linux.
My Code
function init_remote_commands_to_use
{
# Test if the environment variable SSH_FOR_RCOMMANDS is present in .temip_config file,
# use secured on non secured rcommand depending on the result
if [ "$SSH_FOR_RCOMMANDS" != "" ]
then
if [ "$SSH_FOR_RCOMMANDS" = "ON" ]
then
# Check if the environment variable SSH_PATH is specified in .temip_config file
if [ "$SSH_PATH" != "" ]
then
SH_RCMD=$SSH_PATH
else
SH_RCMD=$SSH_CMD
fi
# Check if a ssh-agent is already running
if [ "$SSH_AGENT_PID" = "" ]
then
#Run ssh-agent for secured RCommands
eval `ssh-agent`
ssh-add
STARTEDBYME=YES
fi
else
if [ "$SSH_FOR_RCOMMANDS" = "OFF" ]
then
SH_RCMD=$RSH_CMD
else
echo "Please set the SSH_FOR_RCOMMANDS value to ON or OFF in the .temip_config file"
exit 1
fi
fi
else
SH_RCMD=$RSH_CMD
fi
}
below is the error:
function: Command not found.
{: Command not found.
SSH_FOR_RCOMMANDS: Undefined variable.
Please anyone suggest what I am missing?

The C Shell csh does not have functions. It does have aliases, but those are harder to write and read. For exmaple, see here: https://unix.stackexchange.com/questions/62032/error-converting-a-bash-function-to-a-csh-alias
It might be a good idea to simply switch to Bash, where your existing code may already be working.

Related

linux shell script variable "not found"

Just trying to exit a loop once no input is entered at the prompt, but I'm having trouble testing for the value in an if statement?
CODE:
SQL="?";
while true
do
if [ "$SQL" == "" ]
then
break
else
read -p "SQL: " SQL
clear
php -f sql.php "$SQL"
fi
done
OUTPUT:
sql.sh: 5: [: ?: unexpected operator
SQL:
Although it looks like part of a scripting language, [ is actually the name of a command, also known as test. The if statement runs that command, and acts based on its result. (The same is true, incidentally, of the true in your while loop - it's a command that "does nothing, successfully".)
As such, you need a space between the command and its parameters, as well as between the if and the command. You also need to use the correct arguments; the standard spelling for comparing two strings is = rather than ==
if [ "$SQL" = "" ]
Which is equivalent to:
if test "$SQL" = ""

If statements accepting yes or no in bash script?

I am trying to accept user input of yes or no to a question and depending on the answer read back the value of my variable. I can never get commands attached to variables to work or my if statements to accept yes or no. It just keeps going to "not a valid answer".
Please let me know how to actually get those to work in bash script. I keep lookingup different things to try and nothing seems to work.
Here is what I have now:
yesdebug='echo "Will run in debug mode"'
nodebug='echo "Will not run in debug mode"'
echo "Would you like to run script in debug mode? (yes or no)"
read yesorno
if [$yesorno == 'yes']; then
$yesdebug
elif [$yesorno == 'no']; then
$nodebug
else
echo "Not a valid answer."
exit 1
fi
There are several problems with your code:
Failure to put spaces around [ (which is a command name) and ] (which is a mandatory but functionless argument to ]).
Treating the contents of a variable as a command; use functions instead.
yesdebug () { echo "Will run in debug mode"; }
nodebug () { echo "Will not run in debug mode"; }
echo "Would you like to run script in debug mode? (yes or no)"
read yesorno
if [ "$yesorno" = yes ]; then
yesdebug
elif [ "$yesorno" = no ]; then
nodebug
else
echo "Not a valid answer."
exit 1
fi

BASH Variable to string comparison always fails

I've searched, and searched, and searched... but I just can't figure out why on earth this simple BASH function is failing.
The code:
# Function to quickly disable or enable proxy server, system wide
proxee() {
MODE=$(gsettings get org.gnome.system.proxy mode)
echo $MODE
if [ "$MODE" = "manual" ]
then
gsettings set org.gnome.system.proxy mode 'none'
echo "Proxy Disabled"
elif [ "$MODE" = "none" ]
then
gsettings set org.gnome.system.proxy mode 'manual'
echo "Proxy Enabled"
else
echo "FAIL"
fi
}
Every time I try to run it I get the following output:
'none'
FAIL
I essentially just want to compare the variable I have declared with a string literal.
I am pretty new to bash scripting and I've read over 15 different answers from Stack Overflow (this seems to be a common problem) - but I just can't figure it out!
Any help is much appreciated.
The command gsettings get org.gnome.system.proxy mode returns 'none' including the quote signs (').
Therefore you have to include them into the comparison:
...
elif [ "$MODE" = "'none'" ]
then
...
just change this line:
elif [ "$MODE" = "'none'" ]
string returned to mode is 'none' not none
Enjoy

how to declare variable name with "-" char (dash ) in linux bash script

I wrote simple script as follow
#!/bin/bash
auth_type=""
SM_Read-only="Yes"
SM_write-only="No"
echo -e ${SM_Read-only}
echo -e ${SM_Write-only}
if [ "${SM_Read-only}" == "Yes" ] && [ "${SM_Write-only}" == "Yes" ]
then
auth_type="Read Write"
else
auth_type="Read"
fi
echo -e $auth_type
And when i execute it i got following output with errors.
./script.bash: line 5: SM_Read-only=Yes: command not found
./script.bash: line 6: SM_write-only=No: command not found
only
only
Read
Any one know correct way to declare the variable with "-" (dash)?
EDIT:
have getting response from c code and evaluate the variables for example
RESP=`getValue SM_ Read-only ,Write-only 2>${ERR_DEV}`
RC=$?
eval "$RESP"
from above scripts code my c binary getValue know that script want Read-only and Write-only and return value to script.So during eval $RESP in cause error and in my script i access variable by
echo -e ${SM_Read-only}
echo -e ${SM_Write-only}
which also cause error.
Rename the variable name as follows:
SM_Read_only="Yes"
SM_write_only="No"
Please, don't use - minus sign in variable names in bash, please refer to the answer, on how to set the proper variable name in bash.
However if you generate the code, based on others output, you can simply process their output with sed:
RESP=$(getValue SM_ Read-rule,Write-rule 2>${ERR_DEV}|sed "s/-/_/g")
RC=$?
eval "$RESP"
- is not allowed in shell variable names. Only letters, numbers, and underscore, and the first character must be a letter or underscore.
I think you cant have a dash in your variables names, only letters, digits and "_"
Try:
SM_Read_only
Or
SM_ReadOnly

Shell script showing errors while executing in Linux

SCRIPT :
IMAGE=$imgvalue;
if [ $imgvalue :=1 ]
then
echo DO=ABC;
elif [ $imgvalue :=2 ]
then
echo DO=ETC;
elif [ $imgvalue :=3 ]
then
echo DO=XYZ;
else
echo "$imgvalue is unsupported";
exit 1;
fi
In the script above, IMAGE=1, IMAGE=2, IMAGE=3 whatever may be the value I have assigned. It's showing only DO=ABC. Other conditions not working. Can anyone explain what's wrong with my script?
If $imgvalue is not an empty string, your first test is a syntax error, so I am assuming it is empty in the tests you are doing. In that case, your first test is equivalent to:
if [ :=1 ]
which is always true because :=1 is not an empty string. You probably meant to write:
if [ "$imgvalue" = 1 ]

Resources