Bash script: if any argument is "N" then function has extra options - linux

(I'll reuse this as it's about the same thing).
I have managed to make a usable script that does everything what's explained below the code block, except the order of the arguments, i.e.:
./test.sh B N A
will delete B.zip, create a new archive, but stops there, A will not be processed. It's fine, I can keep N as the last argument, no problem. Also, the echo "no backup needed, removing" does not work for some reason, but that's rather optional.
My question is: can what I have done be improved/altered somehow so that if there are other folders to be added in time the only changes to the script to be the DIRx entries? The biggest problem I see is modifying the block between <start> and <stop>. Here is the script:
#!/bin/bash
DIR1=A
DIR2=B
DIR3=C
bak()
{
if [[ "$*" == N ]]
then
if [ $1 == N ]
then
:
else
echo "no backup needed, removing"
rm -v $1.zip
fi
else
if
[ -f $1.zip ]
then
/bin/mv -vi $1.zip BKP/$1.zip_`/bin/date +"%H-%M"`
else
echo "no "$1".zip"
fi
fi
}
archive()
{
if [ $* == N ]
then
:
else
if [ $1 == C ]
then
7z a -mx=9 $1.zip ../path/$1 -r -x\!$1/nope
else
7z a -mx=9 $1.zip $1 -r -x\!$1/bogus
fi
fi
}
########### <start> ####################
if [ -z "$*" ] || [[ "$#" -eq 1 && "$1" == N ]]
then
bak "$DIR1"
bak "$DIR2"
bak "$DIR3"
archive "$DIR1"
archive "$DIR2"
archive "$DIR3"
fi
############## <stop> #####################
#if [[ "$#" -eq 1 && "$1" == N ]]
#then
# rm -v "$DIR1".zip
# rm -v "$DIR2".zip
# rm -v "$DIR3".zip
# archive "$DIR1"
# archive "$DIR2"
# archive "$DIR3"
#fi
if [[ "$#" -gt 1 && "$*" == *N* ]]
then
while [ "$#" -gt 1 ]
do
if [ "$1" == N ]
then
:
else
rm -v "$1".zip
fi
archive "$1"
shift
done
else
while [ "$#" -ge 1 ]
do
bak "$1"
archive "$1"
shift
done
fi
exit 0
Now, here's what I have and want it to do. The current directory holds the script, test.sh, and the folders A and B. ls -AR produces this:
A B test.sh
./A:
1.txt 2.txt bogus
./B:
3.txt 4.txt bogus
There is another folder, C, in ../path/. The same ls -AR ../path gives this:
../path:
C
../path/C:
5.txt 6.txt nope
../path/C/nope:
q.doc w.rtf
What I want the script to do. When run with no arguments:
./test.sh
1) checks for existing zip archives in the current directory
1.a) if they exist, a backup is made for each with additional date suffix into BKP/.
2.a) if not, it lets you know
2) the three folders, A, B and C are archived, folders A and B without A/bogus and B/bogus and folder C without ../path/C/nope/* and ../path/C/nope/ .
If run with arguments, these can be any of A, B or C, with an optional N. If run with N, only:
./test.sh N
then no archive check/backup will be performed, any archives already existent will be deleted and all 3 folders get archived. If run with any combination of A, B or C, for example:
./test.sh A C
then only archives A.zip and C.zip have a check and backup and only folders A and C are archived, A without A/bogus and C without ../path/C/nope/* and ../path/C/nope/ . If run with any combination of A, B or C, but with additional N, i.e.:
./test.sh B N C
Then no check/backup is performed for B.zip and C.zip, the archives (if existent) get deleted and the folders B and C are archived.
The archives will have (inside) the folder as the root directory (i.e. open up the archive and you'll see A, B or C first) and all three of them have exceptions to the list of files to be processed: A and B don't need bogus, while C doesn't need subfolder none and anything inside it. I use 7z instead of zip because I can write:
7z a x.zip ../path/./C/bla/bla
and have C as the root directory; I couldn't do it with zip (most likely I don't know how to, it doesn't matter as long as it works).
So far, the checking and the backup work. The archiving, if no exceptions are added and I remove the $PATH thing, work. The whole script doesn't. I would have posted every combination I have done so far, but 99% of them would have probably been impossible and the rest childish. I couldn't care less how it looks as long as it does the job.
Very optional: can an alias (or some sort) like "SCF" be made to "Supercalifragilistic"? The C folder has a rather long name (I could just make a symlink, I know). I have no idea about this one.

I managed to do it. It's probably very childish and the worst possible script but I don't care right now, it works. Of course, if somebody has a better alternative to share, it's welcome, until then here's the whole script:
#!/bin/bash
# ./test.sh = 1. searches for existing archives
# 1.a. if they exist, it backups them into BKP/.
# 1.b. if not, displays a message
# 2. archives all the directories in the array list
# ./test.sh N = 1. deletes all the folder's archives existent and
# specified in the array list
# 2. archives all the directories in the array list
# ./test.sh {A..F} = 1. searches for existing archives from arguments
# 1.a. if they exist, it backups them into BKP/.
# 1.b. if not, displays a message
# 2. archives all the directories passed as arguments
# ./test.sh {A..F} N = 1. deletes all the archives matching $argument.zip
# 2. archives all the directories passed as arguments
# The directories to be backed-up/archived, all in the current (script's) path
# except "C", on a different path
DIR=(A B C D E F)
# The back-up function, if any argument is "N", processing it is omitted
bak()
{
if [[ "$*" == N ]]
then
:
else
if
[ -f $1.zip ]
then
mv -vi $1.zip BKP/$1.zip_`date +"%H-%M"`
else
echo "$(tput setaf 1) no "$1".zip$(tput sgr0)"
fi
fi
}
# The archive function, if any argument is "N", processing it is omitted. Folder
# "C" has special treatment
archive()
{
if [ $* == N ]
then
:
else
if [ $1 == C ]
then
7z a -mx=9 $1.zip ../path/$1 -r -x\!$1/nope
else
7z a -mx=9 $1.zip $1 -r -x\!$1/bogus
fi
fi
}
# case #1: no arguments
if [ -z "$*" ]
then
for i in $(seq 0 $((${#DIR[#]}-1))) # counts from 0 to array-1
do
echo "$(tput setaf 2) backup$(tput sgr0)"
bak "${DIR[i]}"
archive "${DIR[i]}"
done
exit $?
fi
# case #2: one argument, "N"
if [[ "$#" -eq 1 && "$1" == N ]]
then
for i in $(seq 0 $((${#DIR[#]}-1)))
do
echo "$(tput setaf 1) no backup needed, removing$(tput sgr0)"
rm -v "${DIR[i]}".zip
archive "${DIR[i]}"
done
exit $?
fi
# case #3: folders as arguments with "N"
if [[ "$#" -gt 1 && "$*" == *N* ]]
then
while [ "$#" -gt 1 ]
do
if [ "$1" == N ]
then
:
else
echo "$(tput setaf 1) no backup needed, removing$(tput sgr0)"
rm -v "$1".zip
fi
archive "$1"
shift
done
# case #4: folders as arguments without "N"
else
while [ "$#" -ge 1 ]
do
echo "$(tput setaf 2) backup$(tput sgr0)"
bak "$1"
archive "$1"
shift
done
fi
exit $?

Related

Script to calculate sum of odd or even file size - Linux Shell Scripting

I need to write a shell script that will count total size of files which size is an
odd or an even number (depending on the parameter given in the command line at the time the
script is executed).
Here is my code:
#!/bin/bash
set -- "."
totsize=0
if [ "$2" == "odd" ]
then
for file in "$1"/*
do
if [ -f "$file" ]
then
size=$(stat -c '%s' "$file")
if ((size % 2 == 1))
then
((totsize += $size))
fi
fi
done
else
for file in "$1"/*
do
if [ -f "$file" ]
then
size=$(stat -c '%s' "$file")
if ((size % 2 == 0))
then
((totsize += $size))
fi
fi
done
fi
echo $totsize
The script should be executed ex. by typing bash script2.sh ./tmp odd
Unfortunately, the script goes directly to the "else" section.
What I have noticed is, that the second argument odd or even is not even passed to the script.
Could you please help me? Thank you in advance.

cp command can't parse a path with wildcard in it

I have a function I wrote in bash that copies files.
It was written so it would be less painful for us to turn our batch scripts that use xcopy to bash scripts. This is because the copy commands in Linux work a little bit different.
The function does several things:
It creates a path to the target directory if it doesn't exist yet.
It uses cp to copy files
it uses cp -r to copy directories.
it uses rsync -arv --exclude-from=<FILE> to copy all the files and folders in a gives directory except the files/folders listed in FILE
The problem is, that when I try to copy files with * it gives me an error:
cp: cannot stat 'some dir with * in it': No such file or directory.
I found out that I can instead write something like that: cp "<dir>/"*".<extension>" "<targetDir>" and the command itself works. But when I try to pass that to my function, it gets 3 arguments instead of 2.
How can I use the cp command in my function while being able to pass a path with wildcard in it? meaning the argument will have double quotes in the beginning of the path and in the end of them, for example: Copy "<somePath>/*.zip" "<targetDir>"
function Copy {
echo "number of args is: $#"
LastStringInPath=$(basename "$2")
if [[ "$LastStringInPath" != *.* ]]; then
mkdir -p "$2"
else
newDir=$(dirname "$2")
mkdir -p "newDir"
fi
if [ "$#" == "2" ]; then
echo "Copying $1 to $2"
if [[ -d $1 ]]; then
cp -r "$1" "$2"
else
cp "$1" "$2"
fi
if [ $? -ne 0 ]; then
echo "Error $? while trying to copy $1 to $2"
exit 1
fi
else
rsync -arv --exclude-from="$3" "$1" "$2"
if [ $? -ne 0 ]; then
echo "Error $? while trying to copy $1 to $2"
exit 1
fi
fi
}
Okay, so I couldn't solve this with the suggestions I was given. What was happening is either the * was expanding before it was sent to function or it wouldn't expand at all inside the function. I tried different methods and eventually I decided to rewrite the function so it would instead support multiple arguments.
The expansion of the wild card happens before it sent to my function, and the copy function does all the actions it was doing before while supporting more than one file/dir to copy.
function Copy {
argumentsArray=( "$#" )
#Check if last argument has the word exclude, in this case we must use rsync command
if [[ ${argumentsArray[$#-1],,} == exclude:* ]]; then
mkdir -p "$2"
#get file name from the argument
excludeFile=${3#*:}
rsync -arv --exclude-from="$excludeFile" "$1" "$2"
if [ $? -ne 0 ]; then
echo "Error while to copy $1 to $2"
exit 1
fi
else
mkdir -p "${argumentsArray[$#-1]}"
if [[ -d $1 ]]; then
cp -r "${argumentsArray[#]}"
if [ $? -ne 0 ]; then
exit 1
fi
else
cp "${argumentsArray[#]}"
if [ $? -ne 0 ]; then
exit 1
fi
fi
fi
}

How to check for a folder in 2 machines using shell script?

I am working on a shell script which I need to run on machineX. It will check for a certain folder which is in this format YYYYMMDD inside this folder MAPPED_LOCATION in other two machines - machineP and machineQ. So the path will be like this in both machineP and machineQ-
/bat/testdata/t1_snapshot/20140311
And inside the above folder path, there will be some files inside in it. Below is my shell script -
#!/bin/bash
readonly MACHINES=(machineP machineQ)
readonly MAPPED_LOCATION=/bat/testdata/t1_snapshot
readonly FILE_TIMESTAMP=20140311
# old code which I was using to get the latest folder inside each machine (P and Q)
dir1=$(ssh -o "StrictHostKeyChecking no" david#${MACHINES[0]} ls -dt1 "$MAPPED_LOCATION"/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | head -n1)
dir2=$(ssh -o "StrictHostKeyChecking no" david#${MACHINES[1]} ls -dt1 "$MAPPED_LOCATION"/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | head -n1)
dir3=$MAPPED_LOCATION/$FILE_TIMESTAMP # /bat/testdata/t1_snapshot/20140311
echo $dir1
echo $dir2
echo $dir3
if dir3 path exists in both the machines (P and Q) and number of files is greater than zero in each machine
then
# then do something here
echo "Hello World"
else
# log an error - folder is missing or number of files is zero in which servers or both servers
fi
Noow what I am supposed to do is - If this path exists /bat/testdata/t1_snapshot/20140311 in both of the machines and number of files are greater than zero in both of the machines, then do somethting. Else if the folder is missing in any of the servers or number of files is zero in any of ther servers, I will exit out of the shell script with non zero status and a message with an actual error.
How can I do this in shell script?
Update:-
for machine in $MACHINES; do
dircheck=($(ssh -o "StrictHostKeyChecking no" david#${machine} [[ ! -d "$dir3" ]] \&\& exit 1 \; ls -t1 "$dir3"))
#On the ssh command, we exit 1 if the folder doesn't exist. We check the return code with `$?`
if [[ $? != 0 ]] ;then
echo "Folder doesn't exist on $machine";
exit 1
fi
# check number of files retrieved
if [[ "${dircheck[#]}" = 0 ]] ;then
echo "0 Files on server $machine";
exit 1
fi
#all good for $machine here
done
echo "Everything is Correct"
If I am adding a new empty folder 20140411 inside machineP and then execute the above script, it always prints out -
echo "Everything is Correct"
Infact, I didn't added any folder in machineQ. Not sure what is the problem?
Another Update-
I have created an empty folder 20140411 in machineP only. And then I ran the script in debug mode -
david#machineX:~$ ./test_file_check_1.sh
+ FILERS_LOCATION=(machineP machineQ)
+ readonly FILERS_LOCATION
+ readonly MEMORY_MAPPED_LOCATION=/bexbat/data/be_t1_snapshot
+ MEMORY_MAPPED_LOCATION=/bexbat/data/be_t1_snapshot
+ readonly FILE_TIMESTAMP=20140411
+ FILE_TIMESTAMP=20140411
+ dir3=/bexbat/data/be_t1_snapshot/20140411
+ echo /bexbat/data/be_t1_snapshot/20140411
/bexbat/data/be_t1_snapshot/20140411
+ for machine in '$FILERS_LOCATION'
+ dircheck=($(ssh -o "StrictHostKeyChecking no" david#${machine} [[ ! -d "$dir3" ]] \&\& exit 1 \; ls -t1 "$dir3"))
++ ssh -o 'StrictHostKeyChecking no' david#machineP '[[' '!' -d /bexbat/data/be_t1_snapshot/20140411 ']]' '&&' exit 1 ';' ls -t1 /bexbat/data/be_t1_snapshot/20140411
+ [[ 0 != 0 ]]
+ [[ '' = 0 ]]
+ echo 'Everything is Correct'
Everything is Correct
What you want to do is, ls the remote directory (remove the -d flag to ls (which lists only folders), and the head -n1 command as it only prints the first file) and retrieve the data in an array variable.
I also added a check for directory existance [[ -d "$dir3" ]] before executing the ls and escaped the && to not be interpreted on the current bash script.
[[ -d "$dir3" ]] \&\& ls -t1 "$dir3"
To define a bash array, add extra ( ) arround the command., then compare the array size.
dir3="$MAPPED_LOCATION/$FILE_TIMESTAMP" # /bat/testdata/t1_snapshot/20140311
for machine in ${MACHINES[*]}; do
dir3check=($(ssh -o "StrictHostKeyChecking no" david#${machine} [[ -d "$dir3" ]] \&\& ls -t1 "$dir3"))
if [[ "${#dir3check[#]}" -gt 0 ]] ;then
# then do something here
echo "Hello World"
else
# log an error - folder is missing or number of files is zero in server $machine
fi
done
UPDATE:
for machine in ${MACHINES[*]}; do
dircheck=($(ssh -o "StrictHostKeyChecking no" david#${machine} [[ ! -d "$dir3" ]] \&\& exit 1 \; ls -t1 "$dir3"))
#On the ssh command, we exit 1 if the folder doesn't exist. We check the return code with `$?`
if [[ $? != 0 ]] ;then
echo "Folder doesn't exist on $machine";
exit 1
fi
# check number of files retrieved
if [[ "${#dircheck[#]}" = 0 ]] ;then
echo "0 Files on server $machine";
exit 1
fi
#all good for $machine here
done
#all good for all machines here

Bash alias utilizing the command inputs

I want to create a bash alias to do the following:
Assume I am at the following path:
/dir1/dir2/dir3/...../dirN
I want to go up to dir3 directly without using cd ... I will just write cdd dir3 and it should go directly to /dir1/dir2/dir3. cdd is my alias name.
I wrote the following alias, but it doesn't work:
alias cdd='export newDir=$1; export myPath=`pwd | sed "s/\/$newDir\/.*/\/$newDir/"`; cd $myPath'
Simply it should get the current full path, then remove anything after the new destination directory, then cd to this new path
The problem with my command is that $1 doesn't get my input to the command cdd
This is a slightly simpler function that I think achieves what you're trying to do:
cdd() { cd ${PWD/$1*}$1; }
Explanation:
${PWD/$1*}$1 takes the current working directory and strips off everything after the string passed to it (the target directory), then adds that string back. This is then used as an argument for cd. I didn't bother adding any error handling as cdwill take care of that itself.
Example:
[atticus:pgl]:~/tmp/a/b/c/d/e/f $ cdd b
[atticus:pgl]:~/tmp/a/b $
It's a little ugly, but it works.
Here's a function - which you could place in your shell profile - which does what you want; note that in addition to directory names it also supports levels (e.g., cdd 2 to go up 2 levels in the hierarchy); just using cdd will move up to the parent directory.
Also note that matching is case-INsensitive.
The code is taken from "How can I replace a command line argument with tab completion?", where you'll also find a way to add complementary tab-completion for ancestral directory names.
cdd ()
{
local dir='../';
[[ "$1" == '-h' || "$1" == '--help' ]] && {
echo -e "usage:
$FUNCNAME [n]
$FUNCNAME dirname
Moves up N levels in the path to the current working directory, 1 by default.
If DIRNAME is given, it must be the full name of an ancestral directory (case does not matter).
If there are multiple matches, the one *lowest* in the hierarchy is changed to." && return 0
};
if [[ -n "$1" ]]; then
if [[ $1 =~ ^[0-9]+$ ]]; then
local strpath=$( printf "%${1}s" );
dir=${strpath// /$dir};
else
if [[ $1 =~ ^/ ]]; then
dir=$1;
else
local wdLower=$(echo -n "$PWD" | tr '[:upper:]' '[:lower:]');
local tokenLower=$(echo -n "$1" | tr '[:upper:]' '[:lower:]');
local newParentDirLower=${wdLower%/$tokenLower/*};
[[ "$newParentDirLower" == "$wdLower" ]] && {
echo "$FUNCNAME: No ancestral directory named '$1' found." 1>&2;
return 1
};
local targetDirPathLength=$(( ${#newParentDirLower} + 1 + ${#tokenLower} ));
dir=${PWD:0:$targetDirPathLength};
fi;
fi;
fi;
pushd "$dir" > /dev/null
}
I agree with mklement0, this should be a function. But a simpler one.
Add this to your .bashrc:
cdd () {
newDir="${PWD%%$1*}$1"
if [ ! -d "$newDir" ]; then
echo "cdd: $1: No such file or directory" >&2
return 1
fi
cd "${newDir}"
}
Note that if $1 (your search string) appears more than once in the path, this function will prefer the first one. Note also that if $1 is a substring of a path, it will not be found. For example:
[ghoti#pc ~]$ mkdir -p /tmp/foo/bar/baz/foo/one
[ghoti#pc ~]$ cd /tmp/foo/bar/baz/foo/one
[ghoti#pc /tmp/foo/bar/baz/foo/one]$ cdd foo
[ghoti#pc /tmp/foo]$ cd -
/tmp/foo/bar/baz/foo/one
[ghoti#pc /tmp/foo/bar/baz/foo/one]$ cdd fo
cdd: fo: No such file or directory
If you'd like to include the functionality of going up 2 levels by running cdd 2, this might work:
cdd () {
newDir="${PWD%%$1*}$1"
if [ "$1" -gt 0 -a "$1" = "${1%%.*}" -a ! -d "$1" ]; then
newDir=""
for _ in $(seq 1 $1); do
newDir="../${newDir}"
done
cd $newDir
return 0
elif [ ! -d "$newDir" ]; then
echo "cdd: $1: No such file or directory" >&2
return 1
fi
cd "${newDir}"
}
The long if statement verifies that you've supplied an integer that is not itself a directory. We build a new $newDir so that you can cd - to get back to your original location if you want.

Linux: How do I compare all files in a directory against each other?

For my homework, I have to check if two files in a directory have the same contents and if so, replace one with a hardlink to the other. My script looks like:
cd $1 # $1 is the directory this script executes in
FILES=`find . -type f`
for line1 in $FILES
do
for line2 in $FILES
do
(check the two files with cmp)
done
done
My problem is that I can not figure out the conditional expression to make sure that the two files are not the same: If the directory has files a,b,c, and d it should not return true for checking a against a. How do I do this?
Edit: So I've got this:
cmp $line1 $line2 > /dev/null
if [ $? -eq 0 -a "$line1" != "$line2" ]
But it counts the files twice: It checks a and b, and then b and a. for some reason, using < with strings does not work.
Edit: I think I figured it out, the solution is to use a \ before the <
Using test, or its alias [:
if [ "$line1" < "$line2" ]
then
check the files
fi
Note that I'm using < here instead of != (which would otherwise work) so that, once you've compared a with b, you won't later compare b with a.
Here is an optimized way to do it that also properly handle files with embedded spaces or similar:
find . -type f -exec sh -c '
compare() {
first=$1
shift
for i do
cmp -s "$first" "$i" || printf " %s and %s differ\n" "$first" "$i"
done
}
while [ $# -gt 1 ]; do
compare "$#"
shift
done ' sh {} +

Resources