Linux Bash for Monthly Archiving - linux

I need help regarding my script on the 1st part is working but when adding more it shows unknown variables lastyear, month
#!/bin/bash
year=$(date +%Y)
lastyear=$(expr $year-1)
month=$(date +%m)
log=$lastyear$month
mkdir -p /root/temp/$(lastyear)
mkdir -p /root/temp/$(lastyear)/$(month)
mv -f *$log* $(archivefolder)/$(lastyear)/$(month)
Error prompt is
./logdate.sh: line 8: lastyear: command not found
./logdate.sh: line 9: lastyear: command not found
./logdate.sh: line 9: month: command not found
But When I include only till line 6 it's working

Do not put the variables in brackets, at the moment the shell is trying to execute the command lastyear and put it into a variable. The below should be fine:
year=$(date +%Y)
lastyear=$(( year-1 ))
month=$(date +%m)
log="$lastyear$month"
mkdir -p "/root/temp/$lastyear"
mkdir -p "/root/temp/$lastyear/$month"
mv -f "*$log*" "$archivefolder/$lastyear/$month"

Related

creating custom command in /usr/bin doesnt work

I have a script I want to be able to execute as a command. I found this post that works for simple scripts, but mine does not seem to work.
my script is:
#!/bin/bash
alias symlinkall="readarray -d '' array < <(find -type f -print0)
printf "%s\n" "${array[#]}" > file.txt
IFS='
'
mkdir all
for i in `cat file.txt`; do
g=`echo "$i" | tr '/' '_'`
f="${g:2}"
#echo "ln -s \"$i\" \"$f\""
ln -s "$PWD/$i" "$PWD/all/$f"
done
rm file.txt all/file.txt"
when I try to run it as a custom command it seems to get messed up on printf as it creates the file but only writes one "n" to it even though the array has correct file paths in it. when I run it line by line everything works just fine.
with set -x i get this output:
+ symlinkall
cat: file.txt: No such file or directory
/usr/bin/symlinkall: line 11: alias: -s: not found
/usr/bin/symlinkall: line 11: alias: "": not found
/usr/bin/symlinkall: line 11: alias: ""
ln -s /media/pi/HDD1/smb-share/Bilder/folder/ /media/pi/HDD1/smb-share/Bilder/folder/all/
done
rm file.txt all/file.txt: not found
Edit: managed to get it to work, apparently in when executed in terminal it cat creates missing files while in a script it does not

Linux script throwing error though it completely looks fine

I have to write Linux script for below question
Write a script that renames files based on the file extension.  The script should prompt the user  for a file extension.  
Next, it should ask the user what prefix to prepend to the file name(s).  By  default the prefix should be the current date in YYYY­MM­DD format.  
So, if the user simply  presses enter the date will be used.  Otherwise, whatever the user entered will be used as the  prefix.  
Next, it should display the original file name and the new name of the file.  Finally, it  should rename the file. 
I wrote below shell script & its throwing error. To me script looks completely fine. Though I am able to write alternative script but Could someone please suggest reason & resolution of error in this script.
Script:
#!/bin/bash
read -p "Please enter a file extension : " EXT
for f in *.${EXT}
do
read -p "Please enter a file prefix (Press ENTER to prefix current Date) :" PREFIX
if [ -z "PREFIX" ]
then
new = "$(date +"%Y-%M-%d")-$(basename ${f})"
mv $f $new
echo "$f renamed to $new"
else
new = "${PREFIX}-${f}"
mv $f $new
echo "$f renamed to $new"
fi
done
Error :
./new.sh: line 13: new: command not found
BusyBox v1.24.2 (2017-05-25 17:33:59 CEST) multi-call binary.
Usage: mv [-fin] SOURCE DEST
or: mv [-fin] SOURCE... DIRECTORY
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY
-f Don't prompt before overwriting
-i Interactive, prompt before overwrite
-n Don't overwrite an existing file
*.png renamed to
[root#localhost ~]#
[root#localhost ~]#
The spaces are spoiling your script during assignment
#new = "$(date +"%Y-%M-%d")-$(basename ${f})"
new="$(date +"%Y-%M-%d")-$(basename ${f})"
also
#new = "${PREFIX}-${f}"
new="${PREFIX}-${f}"
shellcheck is an excellent tool for a basic shell checking

ffmpeg getting syntax error when run inside shell script only.. Why? [duplicate]

This question already has answers here:
Bash: Syntax error: redirection unexpected
(9 answers)
Closed 8 years ago.
I have the following script:
#!/bin/sh
# Use the PhiPack software on our two aligned sets of sequences...
mkdir FcFeABC
cd FcFeABC
../bin/PhiPack/Phi -f ../../Data/Real_Sequences_and_Networks/FcFeABC_alignment.fas -o -v -w 10 -g
cd -
mkdir FcL10
cd FcL10
../bin/PhiPack/Phi -f ../../Data/Real_Sequences_and_Networks/FcL10_alignment.fas -o -v -w 10 -g
cd -
# Use the PhiPack software on the simulated Datasets...
cd ../Data/Simulated_Sequences_and_Networks/Constant_Sex/Theta\ =\ 0.066/Theta\ =\ 0.066/Medium/CutSequences/;
rmus=($(ls -d *.fas))
cd -
absfiles=(../Data/Simulated_Sequences_and_Networks/Constant_Sex/Theta\ =\ 0.066/Theta\ =\ 0.066/Medium/CutSequences/*.fas)
if [ ${#rmus[#]} = ${#absfiles[#]} ]
then
mkdir ${rmus[#]}
for ((i=0; i<${#absfiles[#]}; i++));
do
cd ${rmus[$i]}
.../bin/PhiPack/Phi -f ${absfiles[$i]} -o -v -w 10 -g
cd -
done
else
echo "Error, Number of files created and files to be read differs"
fi
Which hit's an error at line 16:
./runPhiTests.sh: 16: ./runPhiTests.sh: Syntax error: "(" unexpected
Which is this line:
rmus=($(ls -d *.fas))
I don't understand why the '(' is unexpected - it's a simple assignment of the results of ls to an array.
Thanks,
Ben W.
You aren't running it with bash. You are running with /bin/sh from your shebang line #!/bin/sh.
Either run with bash explicitly bash runPhiTests.sh or fix your shebang line #!/bin/bash.
Try to use #!/bin/bash instead of sh.

Create a heredoc that doesn't interpret anything

I'm writing a script for a friend who is not experienced with bash. The script generates a backup script, generates a crontab and runs crontab to create a cron job.
I want to date these backups, so currently the script (what's relevant) is:
cat <<EOF > ~/scheduledBackups/scripts/finalCutScript.bash
mkdir -p ~/scheduledBackups/FinalCut-`date +%a-%b-%d-%Y_%H`
cp -r $BACKUP_DIR/* ~/scheduledBackups/FinalCut-`date +%a-%b-%d-%Y_%H`
EOF
This, however, generates finalCutScript.bash with the date as is when the "installer" script is run.
Is there a way to place exactly that heredoc within finalCutScript.bash? I want to keep everything in one script so that I can use this script framework later.
Elaboration
Expected behaviour:
I want the file that the heredoc is piped into to contain
mkdir -p ~/scheduledBackups/FinalCut-`date +%a-%b-%d-%Y_%H`
cp -r $BACKUP_DIR/* ~/scheduledBackups/FinalCut-`date +%a-%b-%d-%Y_%H`
Actual behaviour
The file generated by that heredoc contains
mkdir -p ~/scheduledBackups/FinalCut-Fri-Aug-05-2016_16
cp -r ~/Documents//* ~/scheduledBackups/FinalCut-Fri-Aug-05-2016_16
You should EOF in heredoc and use $(...) for command substitution:
cat <<-'EOF' >~/scheduledBackups/scripts/finalCutScript.bash
mkdir -p ~/scheduledBackups/FinalCut-$(date +%a-%b-%d-%Y_%H)
cp -r $BACKUP_DIR/* ~/scheduledBackups/FinalCut-$(date +%a-%b-%d-%Y_%H)
EOF
Update: As per OP's comment below you can also escape $ for not expanding a variable in current shell:
BACKUP_DIR='foobar' # variable to be used below in here-doc
cat <<-EOF >~/scheduledBackups/scripts/finalCutScript.bash
mkdir -p ~/scheduledBackups/FinalCut-\$(date +%a-%b-%d-%Y_%H)
cp -r $BACKUP_DIR/* ~/scheduledBackups/FinalCut-\$(date +%a-%b-%d-%Y_%H)
EOF
Above command will use $BACKUP_DIR from your current environment but will add literal $(date +%a-%b-%d-%Y_%H) in the output.

Bash file shows "ln: command not found"

I'm trying to create a bash script to setup my development environment. The script is running as root but I get the error line 11: ln: command not found
#!/bin/bash
#Require script to run as root - doesn't work - syntax error in conditional expression: unexpected token `;'
#if [[ $(/usr/bin/id -u) -ne 0]]; then
# echo "Script must be run as root";
# exit;
#fi
#PHPMyAdmin
PATH="/etc/apache2/sites-available/phpmyadmin.local";
if [ ! -a PATH ]; then
ln -s /home/user/Ubuntu\ One/htdocs/vhosts/phpmyadmin.local PATH;
a2ensite phpmyadmin.local;
fi
PATH=...
Congratulations, you've clobbered how the shell finds commands. Don't do that.
PATH tells the shell where to look for commands. In your case, it looks for ln somewhere in /etc and predictably doesn't find it there.
You should use a different name.

Resources