#!/bin/bash
export folder=`date -d "today - 1 days" '+%Y%m%d'`;
if filename in /r1/test/med_sms/FDA3A; then
result=
if filename = *$folder*; then
result= mv $filename /r1/test/med_sms/FDA3A/test
else
if filename = *$folder*; then
result= mv $filename /r1/test/med_sms/FDA3A/test
elif
done;
Hi..I'm trying to execute the above scrpot but it is throwing the exception as ./test1.sh: line 11: syntax error near unexpected token `done'
./test1.sh: line 11: `done'
Can someone please help on this?
$ help if
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]...
[ else COMMANDS; ] fi
Bash if statements end in fi
You shouldn't be using the ìf elif statement that way, according to the structure of your program this should be looking something like :
export folder=`date -d "today - 1 days" '+%Y%m%d'`;
if filename in /r1/test/med_sms/FDA3A; then
result= something_here
elif filename = *$folder*; then
result= mv $filename /r1/test/med_sms/FDA3A/test
elif filename = *$folder*; then
result= mv $filename /r1/test/med_sms/FDA3A/test
else
result = PUT here the last condition
fi
Related
I have a syntax error, more like a unexpected symbol near a token 'then', but I can't figure it out..
#!/bin/bash
function Functie(){
LINE=1
while read -r CURRENT_LINE; do
CONTOR=1
for word in "$CURRENT_LINE"; do
if[ "$word" == "$2" ];
then
CONTOR=$CONTOR+1
fi
done
if [ "$CONTOR" -eq "$3" ];
then
echo "$CURRENT_LINE"
fi
LINE=$LINE+1
done < "./"$1""
}
Functie "File1.txt" "Ana" "2"
Run your code through ShellCheck to catch several syntax errors.
Correcting them yields:
#!/bin/bash
function Functie(){
LINE=1
while read -r CURRENT_LINE; do
CONTOR=1
for word in $CURRENT_LINE; do
if [ "$word" == "$2" ];
then
CONTOR=$CONTOR+1
fi
done
if [ "$CONTOR" -eq "$3" ];
then
echo "$CURRENT_LINE"
fi
LINE=$LINE+1
done < ./"$1"
}
Functie "File1.txt" "Ana" "2"
One issue it doesn't detect is the bad assignments. To increment a variable write one of these:
CONTOR=$(($CONTOR+1))
CONTOR=$((CONTOR+1))
((CONTOR += 1))
((++CONTOR))
I am new to bash scripting and I have to create this script that takes 3 directories as arguments and copies in the third one all the files in the first one that are NOT in the second one.
I did it like this:
#!/bin/bash
if [ -d $1 && -d $2 && -d $3 ]; then
for FILE in [ ls $1 ]; do
if ! [ find $2 -name $FILE ]; then
cp $FILE $3
done
else echo "Error: one or more directories are not present"
fi
The error I get when I try to execute it is: "line 7: syntax error near unexpected token `done' "
I don't really know how to make it work!
Also even if I'm using #!/bin/bash I still have to explicitly call bash when trying to execute, otherwise it says that executing is not permitted, anybody knows why?
Thanks in advance :)
Couple of suggestions :
No harm double quoting variables
cp "$FILE" "$3" # prevents wordsplitting, helps you filenames with spaces
for statement fails for the fundamental reason -bad syntax- it should've been:
for FILE in ls "$1";
But then, never parse ls output. Check [ this ].
for FILE in ls "$1"; #drastic
Instead of the for-loop in step2 use a find-while-read combination:
find "$1" -type f -print0 | while read -rd'' filename #-type f for files
do
#something with $filename
done
Use lowercase variable names for your script as uppercase variables are reserved for the system. Check [this].
Use tools like [ shellcheck ] to improve script quality.
Edit
Since you have mentioned the input directories contain only files, my alternative approach would be
[[ -d "$1" && -d "$2" && -d "$3" ]] && for filename in "$1"/*
do
[ ! -e "$2/${filename##*/}" ] && cp "$filename" "$3"
done
If you are baffled by ${filename##*/} check [ shell parameter expansion ].
Sidenote: In linux, although discouraged it not uncommon to have non-standard filenames like file name.
Courtesy: #chepner & #mklement0 for their comments that greatly improved this answer :)
Your script:
if ...; then
for ...; do
if ...; then
...
done
else
...
fi
Fixed structure:
if ...; then
for ...; do
if ...; then
...
fi # <-- missing
done
else
...
fi
If you want the script executable, then make it so:
$ chmod +x script.sh
Notice that you also have other problems in you script. It is better written as
dir1="$1"
dir2="$2"
dir3="$3"
for f in "$dir1"/*; do
if [ ! -f "$dir2/$(basename "$f")" ]; then
cp "$f" "$dir3"
fi
done
this is not totally correct:
for FILE in $(ls $1); do
< whatever you do here >
done
There is a big problem with that loop if in that folder there is a filename like this: 'I am a filename with spaces.txt'.
Instead of that loop try this:
for FILE in "$1"/*; do
echo "$FILE"
done
Also you have to close every if statement with fi.
Another thing, if you are using BASH ( #!/usr/bin/env bash ), it is highly recommended to use double brackets in your test conditions:
if [[ test ]]; then
...
fi
For example:
$ a='foo bar'
$ if [[ $a == 'foo bar' ]]; then
> echo "it's ok"
> fi
it's ok
However, this:
$ if [ $a == 'foo bar' ]; then
> echo "it's ok";
> fi
bash: [: too many arguments
You've forgot fi after the innermost if.
Additionally, neither square brackets nor find do work this way. This one does what your script (as it is now) is intended to on my PC:
#!/bin/bash
if [[ -d "$1" && -d "$2" && -d "$3" ]] ; then
ls -1 "$1" | while read FILE ; do
ls "$2/$FILE" >/dev/null 2>&1 || cp "$1/$FILE" "$3"
done
else echo "Error: one or more directories are not present"
fi
Note that after a single run, when $2 and $3 refer to different directories, those files are still not present in $2, so next time you run the script they will be copied once more despite they already are present in $3.
The goal is to create a simple trash utility using a Bourne shell (it's part of an assignment). I am receiving the following error: "line 17: Syntax Error: Unexpected end of file"
I have been staring at the code for a few hours now and I can't see the mistake (probably something simple I am overlooking)
#!/bin/sh
if [$# == 0] ;then
echo "Usage: trash -l | -p | { filename }*"
else
if $1 == '-l'; then
dir $HOME/.trash
else if $1=='-p'; then
rm $HOME/.trash/*
else
for i in ${} ;do
mv i $HOME/.trash
done
fi
fi
Thanks!
This is what I achieved using shellcheck:
#!/bin/sh
if [ $# -eq 0 ] ;then
echo "Usage: trash -l | -p | { filename }*"
else
if [ "$1" = '-l' ]; then
dir "$HOME"/.trash
elif "$1"=='-p'; then
rm "$HOME"/.trash/*
else
for i in ${} ;do
mv "$i" "$HOME"/.trash
done
fi
I have looked at this for about 30 minutes now and can't seem to find the error in this. It happens at my if/else block at the end.
default()
{
for file in /*
do
if [ -f $file ]; then
((filecount++))
elif [ -d $file ]; then
((dircount++))
fi
done
echo The number of files is "$filecount"
echo The number of directories is "$dircount"
}
specific()
{
for file in $param
do
if [ -f $file ]; then
((filecount++))
elif [ -d $file ]; then
((dircount++))
fi
done
echo The number of files is "$filecount"
echo The number of directories is "$dircount"
}
#Variables
declare -a param=$1
declare -i filecount="0"
declare -i dircount="0"
#Action
if [ $param=='-h' ]; then
echo To use this program, enter a directory path after $0 or leave it blank to use current directory.
elif [ $param=='' ]; then
default()
else
specific()
fi
exit 0
Here is the error code. Any help is appreciated.
./countf.sh: line 44: syntax error near unexpected token `else'
./countf.sh: line 44: `else'
I checked your syntax only and found these errors.
function calls. As #Etan Reisner mentioned.
You need spaces around the comparison operator. like [ $param == '-h' ];
you need to double quote your variable. use [ "$param" == '-h' ] instead of [ $param == '-h' ] . Check this for more details. Double quote to prevent globbing and word splitting
I suggest you to test your script here. http://www.shellcheck.net/ I also should do that first instead of manually check your script.
I am developing simple shell script which copy all my present directory files to backup directory which will be exist in present working directory. now i'm getting error when i pass more then one condition in if.
#!/bin/bash
filename=nx.pdf
for i in *;
do
echo $i;
if [ $i == backup || $i == $filename ] ; then
echo "Found backup."
else
echo "Part 2"
cp -rf $i backup
fi
done
I am getting error
asd.sh: line 6: [: missing `]'
asd.sh: line 6: ==: command not found
Part 2
deployee.sh
asd.sh: line 6: [: missing `]'
asd.sh: line 6: ==: command not found
Part 2
The compare operator is = (as defined in POSIX). But == works on some shells as well.
Something like this should work:
if [ $i = backup ] || [ $i = $filename ] ; then
You should quote $i in "". Otherwise you get syntax errors for filenames with blanks.
To be able to use || and && in conditions, you have to use the double square brackets:
if [[ $i == backup || $i == $filename ]] ; then