Bash error: "b.sh: line 52: syntax error: unexpected end of file logout" - macos

So, I wanted this to download a file from my computer, transfer it to my iPhone, install the file there, and respring my phone. For some reason the error you saw in the title pops up. Would be nice if someone could help, thanks.
#!/bin/bash
clear
cd downloads
if [ ! -d ".tmpdl" ]; then
mkdir ".tmpdl"
fi
cd .tmpdl
echo "Enter .deb link:"
read dllink
echo "Enter name of tweak (simple):"
read name
echo "Enter IP of device:"
read IP
echo "Enter your SSH password. If you don't know what this is enter 'alpine' without the apostrophes."
read pass
echo "Sending test file..."
touch test.txt
sshpass -p "$pass" scp test.txt root#"$IP":/var/mobile/Documents
echo "If the transfer was successful (no permission denied error) enter yes or no if it wasn't."
read reply1
if [ $reply1 == no ]; then
echo "Error, wrong credentials, try again."
exit
fi
echo "Okay, attempting to download file..."
curl -o "$name".deb "$dllink"
if [ ! -d "tmp" ]; then
sshpass -p "$pass" ssh root#"$IP" << EOF
cd /
cd var/mobile/Documents
mkdir tmp
EOF
fi
sshpass -p "$pass" scp "$name".deb root#192.168.1.104:/var/mobile/Documents/tmp
echo "Sent file, deleting it from here..."
cd ..
rm -rf .tmpdl
sshpass -p "$pass" ssh root#"$IP" << EOF
cd /
cd var/mobile/Documents
mkdir tmp
mv "$name".deb tmp/
cd tmp
dpkg -i "$name".deb
echo "Clearing caches..."
cd ..
rm -rf tmp
echo "Done installing, respringing now..."
killall backboardd
EOF

These lines:
if [ ! -d "tmp" ]; then
sshpass -p "$pass" ssh root#"$IP" << EOF
cd /
cd var/mobile/Documents
mkdir tmp
EOF
fi
have the problem. The EOF is not at the beginning of the line (indented by one space), so it is not the end of the here-doc, and hence the here-doc is not terminated until the end of the file (where there happens to be a second EOF that's supposed to be terminating a second here-doc), and that generates the error message because the if is not terminated with a fi. Unindent EOF and you should be in business.

Related

SSH into remote computer and compile/run code

I made a script (below) that goes into a remote computer and runs C code on it. This script works perfectly but asks for the password multiple times. How can I make it only ask for the password once?
#!/bin/bash
USER=myusername
COMP=remote_computer_name
OUTPUT=$1
ARGS=${#:2}
CODE_DIR="Dir_$$"
SCRIPT_NAME=$(basename $0)
LAST_CREATED_DIR=$(ls -td -- */ | head -n 1)
#Check if we are on local computer. If so, we copy the
#current directory to the remote run this script on the remote
if [ "${HOSTNAME}" != "${COMP}" ]; then
if [ "$#" -lt 1 ]; then
echo "Incorrect usage."
echo "Usage: ./${SCRIPT_NAME} <compiled_c_output_name> <arg1> <arg2> ... <argN>"
exit
fi
# Check if there is no makefile in the current directory
if [ ! -e [Mm]akefile ]; then
echo "There is no makefile in this directory"
exit
fi
echo "On local. Copying current directory to remote..."
scp -r ./ ${USER}#${COMP}:/ilab/users/${USER}/${CODE_DIR}
ssh ${USER}#${COMP} "bash -s" < ./${SCRIPT_NAME} ${OUTPUT} ${ARGS}
else
echo "On remote. Compiling code..."
cd $LAST_CREATED_DIR
make clean
make all
if [ -e $OUTPUT ]; then
echo "EXECUTING \"./${OUTPUT} ${ARGS}\" ON REMOTE ($COMP)"
./${OUTPUT} ${ARGS}
fi
fi
You can use SSH-Key authentication technique for password less login -
Here are the steps :
Generate RSA key -
ssh-keygen -t rsa
This generates two files under /home/<user>/.ssh/ id_rsa
(Private) and id_rsa.pub (Public)
The second file is your public key. You have to copy the contents of
this file over to the remote computer you want to log into and append
it to /home/<user>/.ssh/authorized_keys or use ssh-copy-id
utility if available (ssh-copy-id username#remote_host)
After this, the authentication is done by the public-private key pair
and you may not require a password henceforth.
You can use sshpass. Here is an example:
sshpass -pfoobar ssh -o StrictHostKeyChecking=no user#host command_to_run
More info, here:
https://askubuntu.com/questions/282319/how-to-use-sshpass

Download a fix number of directories from ftp server

I have an FTP server with thousands of directories. What I want to do is to download a specific number of them (for example, 500 directories) using a shell script. How can I do that? I tried wget with -Q command. For example, "wget -Q25MB", which gives me 25MB of data. The problem is that each folder has a different size. Therefore, using this command will stop the download in the middle of getting a specific folder.
Assuming wget returns an error when the download get interrupted:
#!/bin/bash
to_del= # empty to_del in case you want to copy-paste this to a terminal instead of using a file
username=blablabla
password=blablabla
server=blablabla
printf -v today '%(%Y_%m_%d)T'
# Get the 500 first directory names to download
ftp -n "$server" << EOF | grep -v '^\.\.\?$' | head -n 502 > "to_download_$today.txt"
user $username $password
ls
bye
EOF
# Then, you can download each folder one by one:
while read -r dir; do
if [[ -e $dir ]]; then
echo >&2 "WARNING: '$dir' already exists!"
continue # We don't download or remove it. Manual action needed
fi
if wget "$username:$password#$server/$dir"; then
to_del+=("$dir")
else
# A directory was not successfully downloaded, we delete the temporary files
echo >&2 "WARNING: '$dir' download failed, skipping..."
rm -rf "$dir"
fi
done < "to_download_$today.txt"
# Now, delete the successfully downloaded folders using a single FTP connection
{
printf 'user %s %s\n' "$username" "$password"
for dir in "${to_del[#]}"; do
printf 'del %s\n' "$dir"
done
printf 'bye\n'
} | ftp -i -n "$server"

script a shell script that test that the new user can indeed upload and download files using passive mode of operation in proftpd

I need a shell script that test that the new user can indeed upload and download files using passive mode of operation in proftpd.I had installed proftpd on /tmp and configuration file is /etc/proftpd.conf.
please guys help me out here
#! /bin/bash
#decompress the file
mkdir /opt/$1
cd /opt/$1
wget http://ftp.swin.edu.au/proftpd/distrib/source/proftpd-1.3.3e.tar.gz
tar -xzf proftpd-1.3.3e.tar.gz
#configure it
cd proftpd-1.3.3e
./configure --sysconfdir=/opt
#Make the file
make
#make install the extracted file
make install
#To add ftp user,group
useradd $2
groupadd $2
mkdir /home/$3
passwd $2
echo "User $3 ">> /opt/proftpd.conf
echo "Group $3 ">> /opt/proftpd.conf
echo "DefaultRoot ~ " >> /opt/proftpd.conf
echo "PassivePorts 30000 35000 ">> /opt/proftpd.conf
exit
echo -n "Enter FTP Username : "
read USER
echo -n "Enter password : "
read -s PASSWD
HOST='192.168.1.100'
ftp -n -v $HOST << EOT
ascii
user $USER $PASSWD
prompt
put file.txt
get file.txt
ls -la
bye
EOT

How to execute bash script after rsync

When I deploy on my dev server I use rsync. But after rsync I need to execute a .sh file for "after" deploy operations like clear cache...
Usually I do this via SSH, but if I deploy very often it's boring write:
ssh ...
write password
cd /var/www/myapp/web
./after_deploy.sh
There is a way to do this quickly? This is my rsync.sh:
#!/bin/bash
host=""
directory="/var/www/myapp/web"
password=""
usage(){
echo "Cant do rsync";
echo "Using:";
echo " $0 direct";
echo "Or:";
echo " $0 dry";
}
echo "Host: $host";
echo "Directory: $directory"
if [ $# -eq 1 ]; then
if [ "$1" == "dry" ]; then
echo "DRY-RUN mode";
rsync -CvzrltD --force --delete --exclude-from="app/config/rsync_exclude.txt" -e "sshpass -p '$password' ssh -p22" ./ $host:$directory --dry-run
elif [ "$1" == "direct" ]; then
echo "Normal mode";
rsync -CvzrltD --force --delete --exclude-from="app/config/rsync_exclude.txt" -e "sshpass -p '$password' ssh -p22" ./ $host:$directory
else
usage;
fi;
else
usage;
fi
If instead of using rsync over SSH, you can run an rsync daemon on the server. This allows you to use the pre-xfer exec and post-xfer exec options in /etc/rsyncd.conf to specify a command to be run before and/or after the transfer.
For example, in /etc/rsyncd.conf:
[myTransfers]
path = /path/to/stuff
auth users = username
secrets file = /path/to/rsync.secrets
pre-xfer exec = /usr/local/bin/someScript.sh
post-xfer exec = /usr/local/bin/someOtherscript.sh
You can then do the transfer from the client machine, and the relevant scripts will be run automatically on the server, for example:
rsync -av . username#hostname::myTransfers/
This approach may also be useful because environment variables relating to the transfer on the server can also be used by the scripts.
See https://linux.die.net/man/5/rsyncd.conf for more information.
You can add a command after the rsync command to execute it instead of starting a shell.
Add the following after the rsync command :
sshpass -p "$password" ssh $host "cd $dir && ./after_deploy.sh"

unix bash syntax error near unexpected token 'do'

I created a unix minecraft launcher. it worked perfectly fine just an hour and a half ago (as of 9:30). then I got this:
/home/axium1998/MinecraftMegaLauncher.sh: line 14: syntax error near unexpected token ~'$'do\r''.
/home/Axium1998/MinecraftMegaLauncher.sh: line 14: 'do
I have no idea what caused this.
# If code needs to be changed, just send me a PM saying something like: Project:MinecraftMegaLauncher Line #<line number> = <changed code>
# if it works (I bet it will, but for me to learn xP )it will be replaced/fixed.
export mc=$HOME/.minecraft
export mcB=$HOME/officialBackup
export tekkit=$HOME/.technic
export tekkitB=$HOME/tekkitBackup
export ftb=$HOME/.feedthebeast
export ftbB=$HOME/ftbBackup
export options=("Official" "MagicLauncher" "Tekkit" "FTB" "Backup" "Restore" "Quit")
echo "==========MinecraftMegaLauncher=========="
echo "This currently supports the following launchers: Official, Magic, Tekkit, and FTB, and doing backups as well!"
echo "I (AXIUM1998) am not responsible for data loss/corruption while backing up/restoring. (It is still indev)"
echo "Also, if there is a launcher you want to be in this mega launcher, I will consider implementing them."
echo "BUG: Running restore twice in a row (running restore, then running it again immeditely) will erase all mc data."
cd $HOME
select optL in "${options[#]}"
do
case $optL in
"Official")
echo "Starting the Official launcher..."
java -jar minecraft.jar
;;
"MagicLauncher")
echo "Starting the MagicLauncher..."
java -jar magic.jar
;;
"Tekkit")
echo "Starting the Tekkit launcher..."
java -jar tekkit.jar
;;
"FTB")
echo "Starting the FTB launcher..."
java -jar ftb.jar
;;
"Quit")
echo "Quitting..."
break
;;
"Backup")
echo "Starting the backup..."
echo "Please input your password (Admin needed :( )"
sudo touch dv
sudo rm dv
if [ ! -d $mcB ]; then
sudo mkdir $HOME/officialBackup
fi
if [ ! -d $tekkitB ];then
sudo mkdir $HOME/tekkitBackup
fi
if [ ! -d $ftbB ]; then
sudo mkdir $HOME/ftbBackup
fi
cd $mcB
sudo rm -rf *
cd $tekkitB
sudo rm -rf *
cd $ftbB
sudo rm -rf *
sudo cp -R $mc/* $mcB/
sudo cp -R $tekkit/* $tekkitB/
sudo cp -R $ftb/* $ftbB/
echo "Backup complete"
echo "Making current user owner of files..."
sudo chown -R $USER $mcB
sudo chown -R $USER $tekkitB
sudo chown -R $USER $ftbB
echo "User $USER now can write to backed up folders"
;;
"Restore")
echo "Starting the restoration..."
echo "Admin is, again, required :( "
sudo touch dv
sudo rm dv
cd $mc
sudo rm -rf *
cd $tekkit
sudo rm -rf *
cd $ftb
sudo rm -rf *
cd $HOME
sudo mv $mcB/* $mc/
sudo mv $tekkitB/* $tekkit/
sudo mv $ftbB/* $ftb/
echo "Restore complete"
;;
*)
echo "Invalid operand.";;
esac
done
edit: may not be exact line. I changed it after I last uploaded it
My wild guess is that you converted your script to Windows format (perhaps copying it from Windows) and then you receive this error: unexpected do\r because the \r is unexpected.
Use dos2unix to convert it.

Resources