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/.
Related
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
I don't know a lot about scripting, but I was attempting to write my own.
Context:
I have 2 servers. When server 1 (ubuntu server) automatically adds files to server 2 (synology) (through docker container) permissions of those files are wrong, so some applications I'm running can't access them.
I wanted to write a script that checks for permissions periodically and then changes them to what I want.
I've been messing with it for some days and this is what I've got for now:
#!/bin/bash
shopt -s nullglob
FOLDERS=(/volume1/files/videos/* /volume1/files/photos/*)
for folder in "${FOLDERS[#]}"; do
# [[ -d "$folder" ]]
if [ "$(stat -c '%a' "$folder")" != "755" ] || [ "$(stat -c '%U' "$folder")" != "my_user" ]
then
# echo "Change user permissions of $folder"
chown -R my_user:users "$folder" && chmod 755 -R "$folder"
fi
done
shopt -u nullglob
The problem whit this script is that when files (and not folders) are added it won't detect those files.
So I change the script to this: (find files in directories and the change top directory's permissions recursively)
#!/bin/bash
shopt -s nullglob
FOLDERS=(/volume1/files/videos/* /volume1/files/photos/*)
FILES=(/volume1/files/videos/*/*.mp4 /volume1/files/photos/*/*/*.jpg)
for file in "${FILES[#]}"; do
if [ "$(stat -c '%a' "$file")" != "755" ] || [ "$(stat -c '%U' "$file")" != "my_user" ]
then
# echo "$file" | (cut -d "/" -f5) # --> WORKS PERFECTLY
rootfolder=$(("$file") | (cut -d "/" -f5))
# echo "$rootfolder" # --> ERROR: cannot execute binary file: Exec format error
# chown -R my_user:users "$rootfolder" && chmod 755 -R "$rootfolder"
echo "chown -R my_user:users "$rootfolder" && chmod 755 -R "$rootfolder" "
fi
done
When I add "$file" | (cut -d "/" -f5) into a variable "rootfolder" ("/volume1/files/videos" and "/volume1/files/photos", I'm getting this error when executing the script:
./modify_media_permissions-test.sh: line 12: /volume1/files/videos/my_video.mp4: cannot execute binary file: Exec format error
Tried different thing, but can't resolve it.
Could someone help me further?
I know it is probably not the best and most efficient script, but I'm learning :)
Thanks a lot!
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/"
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.
I've looked around for an answer to this one but couldn't find one.
I have written a simple script that does initial server settings and I'd like it to remove/unlink itself from the root directory on completion. I've tried a number of solutions i googled ( for example /bin/rm $test.sh) but the script always seems to remain in place. Is this possible? Below is my script so far.
#! /bin/bash
cd /root/
wget -r -nH -np --cut-dirs=1 http://myhost.com/install/scripts/
rm -f index.html* *.gif */index.html* */*.gif robots.txt
ls -al /root/
if [ -d /usr/local/psa ]
then
echo plesk > /root/bin/INST_SERVER_TYPE.txt
chmod 775 /root/bin/*
/root/bin/setting_server_ve.sh
rm -rf /root/etc | rm -rf /root/bin | rm -rf /root/log | rm -rf /root/old
sed -i "75s/false/true/" /etc/permissions/jail.conf
exit 1;
elif [ -d /var/webmin ]
then
echo webmin > /root/bin/INST_SERVER_TYPE.txt
chmod 775 /root/bin/*
/root/bin/setting_server_ve.sh
rm -rf /root/etc | rm -rf /root/bin | rm -rf /root/log | rm -rf /root/old
sed -i "67s/false/true/" /etc/permissions/jail.conf
break
exit 1;
else
echo no-gui > /root/bin/INST_SERVER_TYPE.txt
chmod 775 /root/bin/*
/root/bin/setting_server_ve.sh
rm -rf /root/etc | rm -rf /root/bin | rm -rf /root/log | rm -rf /root/old
sed -i "67s/false/true/" /etc/permissions/jail.conf
break
exit 1;
fi
rm -- "$0"
Ought to do the trick. $0 is a magic variable for the full path of the executed script.
This works for me:
#!/bin/sh
rm test.sh
Maybe you didn't really mean to have the '$' in '$test.sh'?
The script can delete itself via the shred command (as a secure deletion) when it exits.
#!/bin/bash
currentscript="$0"
# Function that is called when the script exits:
function finish {
echo "Securely shredding ${currentscript}"; shred -u ${currentscript};
}
# Do your bashing here...
# When your script is finished, exit with a call to the function, "finish":
trap finish EXIT
The simplest one:
#!/path/to/rm
Usage: ./path/to/the/script/above
Note: /path/to/rm must not have blank characters at all.
I wrote a small script that adds a grace period to a self deleting script based on
user742030's answer https://stackoverflow.com/a/34303677/10772577.
function selfShred {
SHREDDING_GRACE_SECONDS=${SHREDDING_GRACE_SECONDS:-5}
if (( $SHREDDING_GRACE_SECONDS > 0 )); then
echo -e "Shreding ${0} in $SHREDDING_GRACE_SECONDS seconds \e[1;31mCTRL-C TO KEEP FILE\e[0m"
BOMB="●"
FUZE='~'
SPARK="\e[1;31m*\e[0m"
SLEEP_LEFT=$SHREDDING_GRACE_SECONDS
while (( $SLEEP_LEFT > 0 )); do
LINE="$BOMB"
for (( j=0; j < $SLEEP_LEFT - 1; j++ )); do
LINE+="$FUZE"
done
LINE+="$SPARK"
echo -en $LINE "\r"
sleep 1
(( SLEEP_LEFT-- ))
done
fi
shred -u "${0}"
}
trap selfShred EXIT
See the repo here: https://github.com/reedHam/self-shred
$0 may not contain the script's name/path in certain circumstances. Please check the following: https://stackoverflow.com/a/35006505/5113030 (Choosing between $0 and BASH_SOURCE...)
The following script should work as expected in these cases:
source script.sh - the script is sourced;
./script.sh - executed interactively;
/bin/bash -- script.sh - passed as an argument to a shell program.
#!/usr/bin/env bash
# ...
rm -- "$( readlink -f -- "${BASH_SOURCE[0]:-$0}" 2> '/dev/null'; )";
Please check the following regarding shell script source reading and execution since it may affect the behavior when a script is deleted while running: https://unix.stackexchange.com/a/121025/133353 (How Does Linux deal with shell scripts?...)
Related: https://stackoverflow.com/a/246128/5113030 (How can I get the source directory of a Bash script from...)
Just add to the end:
rm -- "$0"
Why remove the script at all? As other have mentioned it means you have to keep a copy elsewhere.
A suggestion is to use a "firstboot" like approach. Simply create an empty file in e.g. /etc/sysconfig that triggers the execution of this script if it is present. Then remove that file at the end of the script.
Modify the script so it has the necessary chkconfig headers and place it in /etc/init.d/ so it is run at every boot.
That way you can rerun the script at a later time simply by recreating the trigger script.
Hope this helps.