I tried a simple
if [ 1 == 1 ]; then echo "Hi"; fi >>/projects/ods/Chk.txt
included in the command task but it fails with error code 512....
what am I missing here?
An if/fi block cannot redirect output.
use
if [ 1 == 1 ]; then echo "Hi">>/projects/ods/Chk.txt; fi
As you indicate this is a simple test case, if you need output from a larger block of if/the/else/fi or other logic, you can wrap it all in a process group and redirect that output..
{
if [ 1 == 1 ]; then
echo "Hi"
else
echo "nope"
fi
} >>/projects/ods/Chk.txt
Also, it's likely that using == is a problem. Typically you'd use 1 -eq 1 or other constructs like if true ; then, or if you really want good math comparisons, use if (( 1 == 1 )) ; then ..., but older shells may or may not support the (( ... )) test.
IHTH
Found this while searching for some my queries.
you should use create mode instead of append mode. >> does not work, same happened with me. try this , if u are still facing this and needed an answer. :)
Related
I’m wanting to do basically what this function does. It comes from the zsh manual page and other zsh documentation. I’m trying to “vaguely understand” without diving into the fine details what this function is doing and how it works.
In this case, I understand everything more or less except the _wanted line and in particular, is the tag "dynamic-dirs" any arbitrary tag or does it need to match what the higher level driving functions are looking for? I read briefly about tags and it seems like they would need to match up but I can’t find dynamic-dirs anywhere in any code that I’ve grep’ed. Nor have a found any type of list of tags that are used and what they means except the example of “files” and “directories” mentioned in the first few paragraphs of zshcompsys(1).
zsh_directory_name() {
emulate -L zsh
setopt extendedglob
local -a match mbegin mend
if [[ $1 = d ]]; then
# turn the directory into a name
if [[ $2 = (#b)(/home/pws/perforce/)([^/]##)* ]]; then
typeset -ga reply
reply=(p:$match[2] $(( ${#match[1]} + ${#match[2]} )) )
else
return 1
fi
elif [[ $1 = n ]]; then
# turn the name into a directory
[[ $2 != (#b)p:(?*) ]] && return 1
typeset -ga reply
reply=(/home/pws/perforce/$match[1])
elif [[ $1 = c ]]; then
# complete names
local expl
local -a dirs
dirs=(/home/pws/perforce/*(/:t))
dirs=(p:${^dirs})
_wanted dynamic-dirs expl 'dynamic directory' compadd -S\] -a dirs
return
else
return 1
fi
return 0
}
The other question is about the ’n’ section. It is going to return a reply even if the directory doesn’t exist. Am I reading the code right?
I guess this would be nice if I was going to do mkdir ~p:foodog ?
I'm new to shell scripting and need some help. I am trying to write a script to bounce some servers and I am having a few issues with my if statements. The First and Second one below is giving me a too many arguments error.
For the first one, I am the variable $jmsProcess is a ps -ef | grep command and I only want to go into the if-statement, if this returns some results. This is the same issue for the second one.
In the Third if-statement I want it to check if either of those variables have the value true but this gives me a
if[ [ false || false ] == true ]: command not found
Error.
#Check the JMS process has been killed
if [ $jmsProcess != null ] # SHOULD THIS BE NULL???
then
echo "JMS Process is still running"
$jmsRunning = "true"
fi
#Check the Bone process has been killed
if [ $boneProcess != null ] # SHOULD THIS BE NULL???
then
echo "B-One Process is still Running"
$boneRunning = "true"
fi
if[ [ $jmsRunning || $boneRunning ] == true ] # CHECK THIS FOR QUOTES
then
# $killProcess
fi
null is not a Bash keyword. If you want to check whether a variable is defined, you can do the following:
if [ "${var+defined}" = defined ]
To check whether it's empty or undefined:
if [ -z "${var-}" ]
We don't know how you're setting any of the variable values, (jmsProcess, boneProcess).
Try surrounding all var values like "$var" (with dbl-quotes) and see if the error messages change.
Also there are numerous syntax issues in code visible above. Hard to tell if it is an artifact of posting here, (The code block feature is pretty robust), so I'm going to comment on what I see wrong.
if[ [ false || false ] == true ]: command not found
There are a lot of issues here: false is an shell command. Try typing it on the command line and then do echo $?. you should see 1. true; echo $? will return 0. But the if statements continue or fall-over to the else block based on the last return code (with some special case exceptions).
Also, I'm guessing you're trying to make some sort of reg exp with [ false || false ] == true. Won't work. see below.
You can set status variables to have the value of false (or true) which will be evaluated correctly by the shell.
Also, if[ will give the 'command not found' msg. So by using vars that have the value false, you can do
Try
jmsRunning=false ; boneRunning=true
if [[ ${jmsRunning} || ${boneRunning} ]] ; then
echo both NOT running
else
echo at least 1 is running
fi
Change both to false to see the message change.
Also, null is just a string in a shell script, you probably mean "".
Finally, var assignments cannot have spaces surrounding the '=' sign AND do not use the '$' char at the front when it is on the left hand side of the statment, i.e.
boneRunning=true
I hope this helps.
I'm trying to write a function that will print out the last 3 elements of $PWD, with a '...' beforehand if there are more than 3 elements.
e.g.
/home/nornagon/src --> ~/src
/home/nornagon/src/foo/bar/baz --> ...foo/bar/baz
This is my code so far, but $foo[-3,-1] doesn't work if the array has too few elements in it.
function custom_pwd() {
d=${PWD/#$HOME/\~}
d=(${(s:/:)d})
echo $d[-4,-1]
}
The zsh already has some nifty prompt processing available with print's -P option. This should do the trick:
custom_pwd() {
d=$(print -P '%3~')
case $d in
('~'*|/*) echo "$d";;
(*) echo "...$d"
esac
}
See man zshmisc, section "EXPANSION OF PROMPT SEQUENCES" for the gory details.
Here's what I came up with, though it's not terribly elegant:
function custom_pwd() {
local d slash
d=${PWD/#$HOME/\~}
case $d in
/*) slash=/ ;;
*) slash= ;;
esac
d=(${(s:/:)d})
d[1]=$slash$d[1]
num=$#d
ellipsis=
if (( num > 3 )); then num=3; ellipsis='…'; fi
echo $ellipsis${(j./.)d[-$num,-1]}
}
I'm very new to shell scripting and i've been struggling with the following shell script. I'm posting the script and the commands i used below for your consideration please help me with the mistake i made.
#
#
#
DBG=0
RLS=0
ALL=0
CLN=0
print_help_uu()
{
echo "Usage: $0 -D -R -A -C ";
echo "Where -C clean the debug project builds";
echo " -D to build in DEBUG config";
echo " -R to build in RELEASE config";
echo " -A to build in both configs";
return
}
#
# Main procedure start here
#
# Check for sufficent args
#
if [ $# -eq 0 ] ; then
print_help_uu
exit 1
fi
#
# Function to clean the project
#
clean()
{
if ["$DBG"="1"]; then
echo "Cleaning debug"
if ["$RLS"="1"]; then
echo "cleaning release + debug"
else
echo "This is bad"
fi
fi
if ["$RLS"="1"]; then
echo "Cleaning release "
fi
return
}
while getopts "DRAC" opt
do
case "$opt" in
D) DBG=1;;
R) RLS=1;;
A) DBG=1;RLS=1;;
C) CLN=1;;
\?) print_help_uu; exit 1;;
esac
clean
done
I'm posting the commands i used to run it and the errors i got when using those commands.
----------
./BuildProject.sh -D
./BuildProject.sh: line 36: [1=1]: command not found
./BuildProject.sh: line 46: [0=1]: command not found
-----------
sh BuildProject.sh -D
BuildProject.sh: 63: [1=1]: not found
BuildProject.sh: 63: [0=1]: not found
-----------
sh ./BuildProject.sh -D
./BuildProject.sh: 63: [1=1]: not found
./BuildProject.sh: 63: [0=1]: not found
I tried to solve it in soo many ways and googled a lot before posting here. But all my trials went in vain. Please tell me where i'm doing the mistake since i'm new to shell scripting.
Thanks in Advance.
[ is a command, but you are trying to invoke the command [1=1]. Add some whitespace:
if [ "$DBG" = "1" ]; then
Try to change ["$DBG"="1"] (and similar if statements) into this: [ "$DBG" = "1" ]
i.e. add some space.
i think it's a "SPACE" problem : try
if [ "$DBG" = "1" ]; then
instead of
if ["$DBG"="1"]; then
It's a space issue indeed.
VAR=VALUE
is only for variable declaration in shell, while
VAR = VALUE
is only for variable testing. It's tricky, you just have to get used to it.
It Worked after adding some extra spaces into it. Thank you all. Is it a Scripting rule to put those spaces in between the variables?? I think i ignored that rule. Thanks for your time.
What is the best way to make sure that all the environment variables I need for my script have been set?
Currently, I have multiple if-statements which is not very neat:
if [ -z "$VAR1" ]
then
echo VAR1 not set
exit
fi
if [ -z "$VAR2" ]
then
echo VAR2 not set
exit
fi
if [ -z "$VAR3" ]
then
echo VAR3 not set
exit
fi
Is there a better way?
You can use indirection:
vars="FOO BAR"
for var in $vars
do
[[ -z ${!var} ]] &&
echo "Variable ${var} is empty" ||
echo "The value of variable ${var} is ${!var}"
done
Use a for loop in (set of variables u want). Won't that work?
I have this function in my shell library:
# Check that a list of variables are set to non-null values.
# $#: list of names of environment variables. These cannot be variables local
# to the calling function, because this function cannot see them.
# Returns true if all the variables are non-null, false if any are null or unset
varsSet()
{
local var
for var ; do
eval "[ -n \"\$$var\" ] || return 1"
done
}
I use it in code like this:
varsSet VAR1 VAR2 VAR3 || <error handling here>
you can short them a lot:
[ -z "$FOO" ] && echo "FOO is empty"
[ -z "$BAR" ] && echo "BAR is empty"
a better way:
${FOO:?"FOO is null or not set"}
${BAR:?"BAR is null or not set"}
Of course if the number of variables you are going to test is not low, looping as suggested #Aviator maybe useful to avoid repeating code.
Upon #Aviator answer, I'd like to suggest to define a well commented variable containing a list of your variables-to-be-tested. This way you don't make your code cryptic.
TEST_FOR_IS_SET="FOO BAR BAZ"