How Do I Fix syntax error: unexpected end of file - shell

my code is
#!/bin/sh
clear
if [[ "$1" == "donut" ]]; then
mkdir ~/.deb
wget ``https://github.com/packageash/repo/blob/main/Donut.deb?raw=true ~/.deb
dpkg --install ~/.deb/Donut.deb
rm ~/.deb/Donut.deb
elif [[ "$1" == "nodonut" ]]; then
sudo rm /usr/local/bin/donut
else
echo "Nothing Picked To Install."
so how do i fix it and can i if i just try cause im trying to make a package manager in shell

Well, if you are looking at only for two options and since this script is really simple, I would recommend changing the if to switch case, because the []s can have different behaviors depending if you are using bash/sh/zsh. I've created a simple snippet with that.
#!/bin/bash
DEB_FOLDER="${HOME}/.deb"
case $1 in
donut)
mkdir -p ${DEB_FOLDER} #try to create folder if it doesn't exists
wget https://cdn.glitch.me/ec7fa70f-caec-4ba9-877f-3f809b43f7ea%2FDonut.deb -O ${DEB_FOLDER}/Donut.deb #save the wget downloaded file to ~/.deb/Donut.deb
sudo dpkg --install ${DEB_FOLDER}/Donut.deb #install the deb package"
rm -f ${DEB_FOLDER}/Donut.deb #removes the ~/.deb/Donut.deb file"
;;
nodonut)
sudo rm /usr/local/bin/donut
;;
*)
echo -n "Nothing Picked To Install"
;;
esac

Related

Symlink dotfiles with a script

As many others have done, I want to create a repo to store my dotfile customizations. Instead of doing ln -s manually, I am using the following script to set things up.
#!/bin/bash
set -e
DIR="$HOME/Documents/Dotfiles"
OLDDIR="$HOME/Documents/Other\ Files/Dotfiles_old"
FILES=($HOME/.bash_profile)
echo "Creating $OLDDIR for backup of any existing dotfiles in ~"
mkdir -p "$OLDDIR"
echo "…done"
echo "Changing to the $DIR directory"
cd "$DIR"
echo "…done"
for FILE in "${FILES[#]}"; do
echo "Backup dotfile $FILE from ~/ to $OLDDIR"
cp -L "$HOME/$FILE" "$OLDDIR"
done
for FILE in "${FILES[#]}"; do
echo "copy $FILE from ~ to $DIR."
cp -L "$HOME/$FILE $DIR/"
echo "Creating symlink to $FILE from ~ to $DIR."
ln -sfn "$DIR/$FILE" "$HOME/$FILE";
done
shellcheck source "$HOME/.bash_profile"
When I run this, cp fails because it thinks that .bash_profile isn't there, which obviously isn't the case:
I think my path to the files may be incorrect, although shellcheck reports nothing. What am I forgetting here?
UPDATE: Made another run at this - minus the cp. The one thing I am still unsure of is the use of exit, in particular since I'm already using -e to check for errors.
Shellcheck and bash -n return 0.
#!/bin/bash
set -e
function makeFiles() {
touch .bash_profile \
touch .gitconfig \
touch .gitignore_global
}
function makeLinks() {
ln -sfn ~/Documents/Dotfiles/.bash_profile ~/.bash_profile \
ln -sfn ~/Documents/Dotfiles/.gitconfig ~/.gitconfig \
ln -sfn ~/Documents/Dotfiles/.gitignore_global ~/.gitignore_global \
source ~/.bash_profile
}
read -rp "This may overwrite existing files. Are you sure? (y/n) " -n 1;
echo "";
if [[ $REPLY =~ ^[Yy]$ ]]; then
makeFiles && makeLinks
fi;
Sigh, ln decides that .bash_profile needs to be a directory for some crazy reason.
You're building the path of the dotfile incorrectly - $FILE already contains the full path of the dotfile, and there's no need to prepend $HOME again. Try with this cp command:
cp -L "$FILE $DIR/"

In bash script Symlink are not getting created even though the commands are executed successfully

I am new to RPM Package enhancement/development and working on post-install script.
I want to achieve the symbolic links creation on execution of post-install script but stuck on a issue.
The script execution is working fine for symbolic link creation but for Upgrade part when I check the Symbolic links in "$RPM_pckg_home/bin" they not getting created though the commands are executed successfully.
Here is the sample code;
Original_bin_path=/a/b/c
RPM_pckg_home=/d/e/f
if [[ "$1" -eq 1 ]]; then # 1 for install
cd $RPM_pckg_home/bin
for cmd in `ls Original_bin_path` ; do
ln -s $Original_bin_path/${cmd} ${cmd}
done
elif [[ "$1" -eq 2 ]]; then # 2 for Upgrade
cd $RPM_pckg_home/bin
for cmd in `ls Original_bin_path` ; do
rm ${cmd}
ln -s $Original_bin_path/${cmd} ${cmd}
done
fi
Could you please suggest where would be the issue.
Aside from the possible typo, this is how you should write your loop:
if [[ "$1" -eq 1 ]]; then # 1 for install
for cmd in "$Original_bin_path"/* ; do
ln -s "${cmd}" "$RPM_pckg_home/bin"
done
elif [[ "$1" -eq 2 ]]; then # 2 for Upgrade
for cmd in "$Original_bin_path"/*; do
rm "${cmd}"
ln -s "${cmd}" "$RPM_pckg_home/bin"
done
fi
Instead of iterating over the output of ls, just iterate over the files that match the glob, and modify your rm and ln commands to accommodate the change in the value of $cmd.

Sending a file as a parameter to a container in order to be compiled causes problems

I'm facing some problems when sending a file to a docker container in order to be compiled and executed.
In the folder that I'm running the command I have a "compiler" folder and a file.cpp(a basic hello world"). I'm using this command:
sudo docker run --rm -it -v /compiler/ dockcompiler file.cpp
And this is the output:
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
chgrp: cannot access '/code': No such file or directory
chmod: cannot access '/code': No such file or directory
file does not exist
My Dockerfile:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y gcc g++
ADD entrypoint.sh entrypoint.sh
ADD run-code.sh run-code.sh
ENTRYPOINT ["/bin/bash", "entrypoint.sh"]
entrypoint.sh
prog=$1
uid=$2
if [ -z $1 ]; then
echo "you must provide a file"
exit 1
fi
if [ -z $uid ]; then
uid=10000
fi
echo "127.0.0.1 $(hostname)" >> /etc/hosts
groupadd code
useradd -u "$uid" -G code -d "/compiler" -m codecube
chgrp code /code
chmod 0775 /code
cd /compiler
sudo -u codecube /bin/bash /run-code.sh $prog
And the run-code.sh file(it works if I run it in terminal):
prog=$1
if [ -z $1 ]; then
echo "you must provide a file"
exit 1
fi
if [ ! -f $1 ]; then
echo "file does not exist"
exit 1
fi
extension="${prog##*.}"
case "$extension" in
"cpp")
g++ $prog && ./a.out
;;
"c")
gcc $prog && ./a.out
;;
"go")
go run $prog
;;
"pl")
perl $prog
;;
"py")
python $prog
;;
"rb")
ruby $prog
;;
*)
echo "invalid language"
exit 1
;;
esac
Updated:
The problem is -m codecube where the it says codecube directory already exists. Also, the /code directory is not present.
If the user directory is already in place on your docker image (assuming a modified image being used here). Then just skel files copy should do the trick and you can get away without doing useradd. A post here offers more info on this.
#copy skel files
cp -r /etc/skel/. /codecube

Syntax error: "(" unexpected in bash script

EDIT: I have fixed my script. It seems to be working. If anyone has any suggestions for improvement I would love the help. Apparently I needed to run it using bash instead of sh.
Here is the updated script:
#!/bin/bash
for file in /home/corey/box/*/*
do
dir=$(basename $"(dirname "$file")")
sudo chmod 0777 /var/log/torrentwatch.log
sudo chmod -R 0777 /home/corey/box/*/*
if [[ "$file" = /home/corey/box/*/*.torrent ]]
then
echo "[$(date)]" "$file added to queue." >> /var/log/torrentwatch.log
/usr/bin/transmission-remote localhost:9091 --auth=transmission:transmission -w /media/Media/Torrents/"$dir" -a "$file"
sleep 40 && rm "$file"
sleep 3 && sudo chmod -R 777 /media/Media && sudo chown -R debian-transmission:debian-transmission /media/Media/info
fi
done
The script is for adding torrent files to a folder and having them added to transmission. Here's the original version of the script:
#!/bin/bash
for file in /home/me/box/*/*
do
dir=$(basename $(dirname "$file"));
sudo chmod 0777 /var/log/torrentwatch.log
sudo chmod -R 0777 /home/me/box/*/*
if "$file" = "/home/me/box/*/*.torrent"; then
echo [`date`] "$file" added to queue. >> /var/log/torrentwatch.log
/usr/bin/transmission-remote localhost:9091 --auth=transmission:transmission -l -w /media/Media/Torrents/$dir -a "$file"
sleep 40 && rm "$file"
sleep 3 && sudo chmod -R 777 /media/Media && sudo chown -R debian-transmission:debian-transmission /media/Media/info
fi
done
The problem is that when I run the script I get
/home/me/box/TV/Name.of.file.torrent: Syntax error: "(" unexpected
I've tried running the script with bash, sh, and zsh, and none seem to work. I can't figure out what the problem is.
This is the immediate problem:
if "$file" = "/home/me/box/*/*.torrent"
It's running the following:
/home/me/box/TV/Name.of.file.torrent = "/home/me/box/*/*.torrent"
...which is to say, it's trying to start the .torrent file as a script (with its first argument being = and its second argument being /home/me/box/*/*.torrent), which generates a syntax error. Instead, use:
if [[ $file = /home/me/box/*/*.torrent ]]
There are other issues elsewhere in this script -- I strongly recommend running it through http://shellcheck.net/.

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