I have this shell script which takes 2 parameters but when i run this it shows me this error.What do I need to do to write this if-then-else block correctly?
NK_BILLED_UNBILLED_DATA.sh: line 31: syntax error near unexpected token else'
'K_BILLED_UNBILLED_DATA.sh: line 31: else
Shell Script
echo Script Name: "$0"
echo Total Number of Argument Passed: "$#"
echo Arguments List -
echo 1. $2
if test -d $2;then
echo "Welcome $2"
else
echo "Not Exist";
fi
echo All Arguments are: "$*"
ORACLE_SID=BSCS01U
export ORACLE_SID
export PATH=.:$PATH:$ORACLE_HOME/bin
export BC=$1
echo "$BC"
if [ $# -ne 0 ];then
export REPORT_GEN_PATH=$2;
else
export REPORT_GEN_PATH=$WORK/Reports/ANT001A_REQ01/generated_reports;
fi
export REPORT_HOME=$BSCS_WORKDIR/Reports/ANT001A_REQ01
echo "Please wait....The Billed and Unbilled report is being created."
sqlplus SYSADM/SYSADM#$ORACLE_SID << eof_disp > $REPORT_HOME/logs/log_file_$$.log
#$REPORT_HOME/scripts/GEN_BILLED_UNBILLED_REPORT_FILE.sql
eof_disp
echo "Billed and Unbilled report created."
Related
I wrote a little Bash script called cs210_list.sh, and it is giving me the following errors when i run it either as bash cs210_list.sh or as
sh cs210_list.sh in linux:
cs210_list.sh: line 2: [0: command not found
cs210_list.sh: line 21: syntax error near unexpected token `fi'
cs210_list.sh: line 21: ` fi'
this is my very first bash script ever. I can't figure why I'm getting the errors. BTW, "(line 21)" is not actually in my code, it's just there for your reference.
$? -eq 0 This command is supposed to determine if there as a argument
#!/bin/bash
if [$? -eq 0];
then
echo "Error: Missing argument";
exit 1
fi
if [-e "$1"];
then
if [-d "$1"];
then
for i in $(ls)
do
if [-d "$i"];
then
echo "File: $i"
echo ""
else
echo "File: $i"
echo " Size: " $(stat -c%s $i)
fi
fi (line 21)
else
echo "$Error: File doesn't exist"
fi
done
I recently started writing BASH scripts, and I am currently trying to practice using while loops. However, when I run the following block of code, the command prompt responds with:
run.command: line 12: syntax error near unexpected token `done'
run.command: `done'
Then the program shuts off.
This is the code I am running.
#!/bin/bash
echo -e "text"
c=false
while true; do
printf ">> "
i=read
if [$i = "exit"]; then
exit
else if [$i = "no"]; then
echo "no"
else
echo -e "Error: $i is undefined"
fi
done
I did some research on while loops, however my loop syntax seems correct. When I remove the done at the end, and Unexpected end of file error occurs. Any help would be appreciated!
You can use the -p option of read for the prompt and a case ... esac construction:
while true; do
read -r -p ">> " i
case "$i" in
"exit") exit 0 ;;
"no") echo "no" ;;
*) echo -e "Error: $i is undefined";;
esac
done
I fixed it myself!
#!/bin/bash
echo -e "text"
c=false
while true; do
printf ">> "
read i
if [ "$i" = "exit" ]; then
exit
elif [ "$i" = "no" ]; then
echo "no"
else
echo -e "Error: $i is undefined"
fi
done
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
I'm having an issue with a script that I am trying to program. Narrowed down and simplified code and it gives an error that command is not found. If i do "test -f file" in command line it returns nothing, not command not found
PATH=$1
#!/bin/bash
DIR=$1
if [[-f $PATH]]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
exit
Here is the actual more complicated script I'm trying to run
verify()
{
if [[-f $1]]; then
VFY[$2]="f"
echo "$1 is a file"
elif [[-d $1]]
then
VFY[$2]="d"
echo "$1 is a directory"
else
VFY[$2]=0
echo -e "\r"
echo "$1 is neither a file or a directory"
echo -e "\r"
fi
}
Its part of a larger script that can move things around depending on inputs. I've run this in CentOS 6, and FreeBSD, both give the same error "[[-f: Command not found"
Simply add an extra space between [[ and -f, and also before ]].
You will get:
#! /bin/bash
DIR=${1-} # unused in your example
if [[ -f test.sh ]]; then
echo "expression evaluated as true"
else
echo "expression evaluated as false"
fi
exit
and for your function
verify() # file ind
{
local file=$1 ind=$2
if [[ -f "$file" ]]; then
VFY[ind]="f" # no need of $ for ind
echo "$file is a file"
elif [[ -d "$file" ]]; then
VFY[ind]="d"
echo "$file is a directory"
else
VFY[ind]=0
echo -e "\n$file is neither a file or a directory\n"
fi
}
I have test.sh which has multiple return condition and test1.sh just echo statement.When i run test2.sh my logic should run test.sh process the file i.e "File successfully" and call test1.sh script .It should not run the test1.sh script when other else condition was executed.i.e "File not successfully", "Input file doesn't exists in directory"
The problem i am facing is when it is executing other condition like "File not successfully", "Input file doesn't exists in directory" it is not retuning "1" as specified exit code but in turn returning 0 i.e from the OS means job was successful. So i am getting "0" from test.sh for all the different condition so test1 .sh is getting called irrespective if the file processed failed etc.Pls advice with the return code
test.sh
FILES=/export/home/input.txt
cat $FILES | nawk -F '|' '{print $1 "|" $2 "|" }' $f > output.unl
if [[ -f $FILES ]];
then if [[ $? -eq 0 ]]; then
echo "File successfully"
else
echo "File not successfully"
exit 1
fi
else
echo "Input file doesn't exists in directory" exit 1 fi
========================================================================
test1.sh
echo "Input file exists in directory"
test2.sh
echo "In test2 script"
./test.sh
echo "return code" $?
if [[ $? -eq 0 ]]; then
echo "inside"
./test1.sh
fi
You're overwriting $? when you use it in echo - after that, it contains the exit code of echo itself. Store it in a variable to avoid this.
echo "In test2 script"
./test.sh
testresult=$?
echo "return code" $testresult
if [[ $testresult -eq 0 ]]; then
echo "inside"
./test1.sh
fi
Edited to add: it's hard to tell what you want from test.sh as the code you pasted is incomplete and doesn't even run. It looks like you meant the cat to be inside the if, because otherwise it errors when the input file is missing, and your $? test does nothing. So I rearranged it like this:
FILES=input.txt
if [[ -f $FILES ]]; then
cat $FILES | awk -F '|' '/bad/{ exit 1 }'
if [[ $? -eq 0 ]]; then
echo "File processed successfully"
else
echo "File processing failed"
exit 1
fi
else
echo "Input file doesn't exist in directory"
exit 1
fi
I've changed the awk script to demonstrate the conditions all work: now, if I put the word bad in input.txt you'll see the "File processing failed" message, otherwise you see success; remove the file and you'll see the input file doesn't exist message.
How do i check for the correct number of arguments (one argument). If somebody tries to invoke the script without passing in the correct number of arguments, and checking to make sure the command line argument actually exists and is a directory.
#!/bin/sh
if [ "$#" -ne 1 ] || ! [ -d "$1" ]; then
echo "Usage: $0 DIRECTORY" >&2
exit 1
fi
Translation: If number of arguments is not (numerically) equal to 1 or the first argument is not a directory, output usage to stderr and exit with a failure status code.
More friendly error reporting:
#!/bin/sh
if [ "$#" -ne 1 ]; then
echo "Usage: $0 DIRECTORY" >&2
exit 1
fi
if ! [ -e "$1" ]; then
echo "$1 not found" >&2
exit 1
fi
if ! [ -d "$1" ]; then
echo "$1 not a directory" >&2
exit 1
fi
cat script.sh
var1=$1
var2=$2
if [ "$#" -eq 2 ]
then
if [ -d $var1 ]
then
echo directory ${var1} exist
else
echo Directory ${var1} Does not exists
fi
if [ -d $var2 ]
then
echo directory ${var2} exist
else
echo Directory ${var2} Does not exists
fi
else
echo "Arguments are not equals to 2"
exit 1
fi
execute it like below -
./script.sh directory1 directory2
Output will be like -
directory1 exit
directory2 Does not exists
You can check the total number of arguments which are passed in command line with "$#"
Say for Example my shell script name is hello.sh
sh hello.sh hello-world
# I am passing hello-world as argument in command line which will b considered as 1 argument
if [ $# -eq 1 ]
then
echo $1
else
echo "invalid argument please pass only one argument "
fi
Output will be hello-world