I am trying to perform a set of terminal operations using bash shell script. Below is my code
#!/bin/bash
FILE_DATE=`date '+%Y%m%d'`
ARCHIVE_DIR="/home/tanmay/backup/"
TAR_GZ=".tar.gz"
PATH=( "/home/tanmay/Downloads/apache-tomcat-7.0.69/logs" "/home/tanmay/Downloads/apache-tomcat-7.0.69/webapps" )
FOLDER=("logs" "webapps" )
for number in {0..1..1}
do
echo ${PATH[number]}
echo ${FOLDER[number]}
rsync -vrzh ${PATH[number]} ${ARCHIVE_DIR}
tar -zcvf ${ARCHIVE_DIR}/${FOLDER[number]}${TAR_GZ} ${ARCHIVE_DIR}/${FOLDER[number]}
rm -rf ${ARCHIVE_DIR}/${FOLDER[number]}
if [ -f ${ARCHIVE_DIR}/${FOLDER[number]}${TAR_GZ} ]
then
mv ${ARCHIVE_DIR}${FOLDER[number]}${TAR_GZ} ${ARCHIVE_DIR}${FOLDER[number]}_${FILE_DATE}${TAR_GZ}
fi
done
When I run this script, both the echo is showing the correct values. But operations (rsync,tar..) are returning file not found. Below is the output
/home/tanmay/Downloads/apache-tomcat-7.0.69/logs
logs
./server_data_backup_updated.sh: line 11: rsync: No such file or directory
./server_data_backup_updated.sh: line 12: tar: No such file or directory
./server_data_backup_updated.sh: line 13: rm: No such file or directory
/home/tanmay/Downloads/apache-tomcat-7.0.69/webapps
webapps
./server_data_backup_updated.sh: line 11: rsync: No such file or directory
./server_data_backup_updated.sh: line 12: tar: No such file or directory
./server_data_backup_updated.sh: line 13: rm: No such file or directory
UPDATE 1
Using one array instead on two. It is working now.
#!/bin/bash
FILE_DATE=`date '+%Y%m%d'`
ARCHIVE_DIR="/home/tanmay/backup/"
TAR_GZ=".tar.gz"
array=( "/home/tanmay/Downloads/apache-tomcat-7.0.69/logs"
"logs"
"/home/tanmay/Downloads/apache-tomcat-7.0.69/webapps"
"webapps")
for number in {0..2..2}
do
rsync -vrzh ${array[number]} ${ARCHIVE_DIR}
tar -zcvf ${ARCHIVE_DIR}/${array[number+1]}${TAR_GZ} ${ARCHIVE_DIR}/${array[number+1]}
rm -rf ${ARCHIVE_DIR}/${array[number+1]}
if [ -f ${ARCHIVE_DIR}/${array[number+1]}${TAR_GZ} ]
then
mv ${ARCHIVE_DIR}${array[number+1]}${TAR_GZ} ${ARCHIVE_DIR}${array[number+1]}_${FILE_DATE}${TAR_GZ}
fi
done
When you reset PATH, the shell can no longer find the executable rsync. When the shell reads the word rsync, it looks through the variable named PATH (which it expects to be a colon separated list of directories, not an array) for a file named rsync. Similarly for tar and rm. The error messages you see are simply telling you that those commands are not found in your PATH.
Related
This a really short question. But is there something syntatically wrong with placing a variable $example as an argument for tar in a bash file?
I have the file written as
//only portion that really matters
#!/bin/bash
...
tar -cvpzf $filename $backup_source
//here's the actual code
#!/bin/bash
backup_source="~/momobobo"
backup_dest="~/momobobo_backup/"
dater=`date '+%m-%d-%Y-%H-%M-%S'`
filename="$backup_dest$dater.tgz"
echo “Backing Up your Linux System”
tar -cvpzf $filename $backup_source
echo tar -cvpzf $filename $backup_source
echo “Backup finished”
//and heres the error
“Backing Up your Linux System”
tar: ~/momobobo: Cannot stat: No such file or directory
tar (child): ~/momobobo_backup/07-02-2013-18-34-12.tgz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
tar -cvpzf ~/momobobo_backup/07-02-2013-18-34-12.tgz ~/momobobo
Notice the "echo tar ...". When I copy and paste the output and run it in my terminal there is no problem taring the file. I'm currently running Xubuntu and I already did an update.
~ doesn't expand to your home directory in double quotes.
Just remove the double quotes:
backup_source=~/momobobo
backup_dest=~/momobobo_backup/
In cases where you have things you would want to quote, you can use ~/"momobobo"
im gong to compile oracle forms on Solaris and create a script.
the script should check if .fmx is created then removes .err file.
here is my script but I've received below error
Code to remove error files
export FORMS_PATH=export FORMS_PATH=/apps/apps/frmcompile/cmteam/hla
for FILE in `ls $FORMS_PATH/*.fmx`; do
if exist "$FILE/*.fmx";
then
rm $FILE/err
fi
done
Error Encountered
rmerr.sh[3]: exist: not found [No such file or directory]
Regular File test is done using "-f"
export FORMS_PATH=export FORMS_PATH=/apps/apps/frmcompile/cmteam/hla
for FILE in `ls $FORMS_PATH/*.fmx`; do
# True if file exists and is a regular file.
if [ -f "$FILE/*.fmx"]; then
rm $FILE/err
fi
done
This might be what you want to do, but it is unclear where .fmx and .err files are located:
export FORMS_PATH=/apps/apps/frmcompile/cmteam/hla
for FILE in $FORMS_PATH/*.fmx; do
b=$(basename $FILE)
[ -f "$b" ] && rm ${b%fmx}err
done
".err" is a file, but you list "err" here.
Some other problem here:
export FORMS_PATH=export FORMS_PATH=/apps/apps/frmcompile/cmteam/hla
Replace with "FORMS_PATH=/apps/apps/frmcompile/cmteam/hla"
for FILE in ls $FORMS_PATH/*.fmx; do
FILE contains every file ending in ".fmx"
if exist "$FILE/.fmx";
Result eg in "/apps/apps/frmcompile/cmteam/hla/blaba.fmx/.fmx" with shell expansion and "exist" - what's this - try "test" or "[]".
rm $FILE/err
Results in "/apps/apps/frmcompile/cmteam/hla/blaba.fmx/err or .err in subfolder and that you don't like, or?
So best use this:
#!/bin/sh OR #!/bin/bash
FORMS_PATH=/apps/apps/frmcompile/cmteam/hla
for fmx in $FORMS_PATH/*.fmx; do
# remove your files ending in .err instead of .fmx
/bin/rm "${fmx%.fmx}.err # only valid with bash
done
Tom
Quite simply, I have a link to dart2js in my /usr/local/bin/ which throws repeated erros when run.
I ran
sudo ln -s /Users/macbook/Development/dart/dart-sdk/bin/dart2js /usr/local/bin/dart2js
When running dart2js from terminal, I'm presented with
...
/usr/local/bin/dart2js: line 9: 1=/Users/macbook/Development/dart/dart-sdk/bin/dart2js: No such file or directory
/usr/local/bin/dart2js: line 9: 1=/Users/macbook/Development/dart/dart-sdk/bin/dart2js: No such file or directory
/usr/local/bin/dart2js: line 9: 1=/Users/macbook/Development/dart/dart-sdk/bin/dart2js: No such file or directory
/usr/local/bin/dart2js: line 9: 1=/Users/macbook/Development/dart/dart-sdk/bin/dart2js: No such file or directory
/usr/local/bin/dart2js: line 9: 1=/Users/macbook/Development/dart/dart-sdk/bin/dart2js: No such file or directory
... etc
I imagine I'm simply using links incorrectly, but I'm not knowledgable enough to know why.
I run into this a while ago and posted this issue Can't run dart command line script using a symlink
I also added a workaround which may help in your situation as well - you probably need to adapt it a bit but it should get you started.
A workaround I use now is to create a bash script named 'testscript' in the same directory as testscript.dart and link to this script instead
I lookup the current directory of the bash script and start the dart script
(Getting the source directory of a Bash script from within).
#!/bin/bash
ME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
${DIR}/${ME}.dart ${BASH_SOURCE[0]}
Instead of getting the command's (symlinks) name with io.Platform.script I have to pass it as an argument to have it available inside the dart script (I use the name of the symlink used to start the script like an argument inside the dart script)
This way I have to distinguish if the script was started directly and use io.Platform.script and args[0] otherwise.
I think this should all be easier.
I need to write a basic program which would find the files which have odd (uneven) size in bytes in user specified directory and then rename them. I wrote a code but can't figure it out what's wrong with it since I have only just began to programm bash scripts... I have 3 files in my directory and here are the errors I'am getting for them:
./Untitled: line 18: AppIcon.icns: command not found
mv: cannot stat ‘AppIcon.icns’: No such file or directory
./Untitled: line 18: AssociatedVm.txt: command not found
mv: cannot stat ‘AssociatedVm.txt’: No such file or directory
./Untitled: line 18: Info.plist: command not found
mv: cannot stat ‘Info.plist’: No such file or directory
My script Code:
#!/bin/bash
n=0
echo “Specify directory”
read directory
if [ -d $directory ]; then
echo “Directory found”
else
echo “Directory not found”
exit 0
fi
for file in $( ls $directory );
do
fsize=$(stat "$directory/$file" -c %s)
if [ $((fsize%2))=1 ]; then
mv "$directory/$file" "$directory/$file.odd"
n=$((n + 1))
fi
done
echo ”Number of renamed files: $n ”
I think you meant
fsize=$(stat "$file" -c %s)
but you wrote
fsize=stat "$file" -c %s
Also, you need to use the absolute path($directory/$file) instead of $file alone if you are running the script from a directory which is not $directory.
Bash uses -eq for integer comparison, so you should also change
if [ $((fsize%2))=1 ]; then
to
if [ $((fsize%2)) -eq 1 ]; then
What is the -c %s for? I don't see a -c option in the stat man page. Did you mean -f? (EDIT: Ok I was looking at the Mac stat command (which is BSD). The stat in GNU version uses -c for format specification)
I need your help so as to create some tarballs, so as to group some files by year. I am using the following script but I get the error message:
tar: 2067_*.inp: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors
Code:
for i in `seq 1960 2100` ; do
tar cvf ${i}_74_1.tar ${i}_*.inp
done
Where the *.inp files have the following structure: 1960_smt.inp, 1960_smt1.inp, etc.
I understand that my error is the * symbol that can't "understand" that I want to take any character. Could someone please help me fix it?
2067_*.inp: Cannot stat: No such file
or directory tar
Sounds more like you don't actually have any files named 2067_XXXX.inp for tar to archive
You'll likely want to check for a matching file to the pattern before you attempt to tar it up:
#!/bin/bash
shopt -u nullglob
for i in {1960..2100}; do
[ -f ${i}_*.inp ] && tar cvf ${i}_74_1.tar ${i}_*.inp
done
P.S.
Does anybody know why replacing [ with [[ ]] as in [[ -f ${i}_*.inp ]] breaks the pattern matching?