PDA

View Full Version : .sh script problem



chris_w3
January 8th, 2015, 09:34 PM
I'm getting this error when trying to execute the following script;

259105

It is giving me a syntax error stating that a 'do' is expected however there is already one there?



#script1.sh
#!/bin/bash
echo Source Folder
if [ -d "$1" ]
then
cd $1
for f in $1/*
do
procFiles "$f"
done
else
echo Working directory not found.
fi
# Wait for a key press ...
read -p "Press ENTER to continue"
clear

echo Target Folder
if [ -d "$1\target" ]
then
cd $1
echo Directory Found.
else
echo Working directory not found.
echo Creating directory…
cd $1
mkdir target
fi
# Wait for a key press ...
read -p "Press ENTER to continue"
clear



#procFiles.sh
#!/bin/bash
ls -l "$1"
read -p "Copy(yes/no)?" yn
if [ "$yn" == "yes" ]
then
cp "$1" $1\target
chmod 444 $1
echo $1 Copied to target folder, file set to read only
else
echo $1 Skipped
fi


Any help would be highly appreciated

Thanks

Vaphell
January 9th, 2015, 12:05 AM
you can copy from terminal using ctrl+shift+c (or middleclick to paste active selection), no need to use screenshots

also your hashbang line has to be in the very first line to be of any use, freestyle comments following. Then even if you had it done correctly, you override the default by calling sh explicitly in terminal (bash =/= sh). Make use of that chmod +x and call the script directly via

./nameofthescript.sh

Indentation, seriously. The code is unreadable because there are no visible code blocks.


if something something
then
for x in ...
do
stuff
done
else
stuff
moar stuff
fi

quote your $variables to prevent nasty word splitting on possible spaces

cd $1
for f in $1/*

in linux / is used in paths, i don't this is going to work

cp "$1" $1\target


final question: were your scripts ever touched by windows text editors?

schragge
January 9th, 2015, 12:37 PM
final question: were your scripts ever touched by windows text editors?
+1. Looks like the script file has Windows-style line breaks. Check it with

cat -v script1.sh If the output shows ^M at end of lines this is the case. You can remove them with

sed -i 's/\r$//' script1.sh