I wrote a shellscript as follows, to check for a file in a samba share:
date_gen=$(date --date="3 days ago" +"%-Y%m%d")
fileName=${date_gen}"_Combined Reg Report.xlsx"
if [ ! -f smb://nfs/carboard/"${fileName}" -U ]
then
echo "File does not exist in Bash"
else
echo ${fileName}
fi
exit 1
Can someone please help me what is wrong with this, I am always getting "File does not exist in Bash". File is there in the folder.
Thanks,
Art
You should check if it's mounted and then check for the file
if mount | grep -q /nfs/cardboard
then
if [[ ! -f /nfs/cardboard/"${fileName}" ]]
then
...
fi
else
echo "not mounted"
fi
Checking the existence of a file with smbclient:
filename="$(date --date='3 days ago' '+%Y%m%d')_Combined Reg Report.xlsx"
if smbclient -A smbauth.conf '//nfs/carboard' -c "ls \"$filename\"" > /dev/null 2>&1
then
echo the file exists
else
echo the file is not there
fi
where smbauth.conf is a file storing your credentials in the following format:
username=myuser
password=mypassword
domain=MYDOMAIN
I don't know how escaping exactly works with smbclient (it seems like some characters like " are impossible to escape), but in your case, double-quoting is enough.
Related
I've installed Clamav on my webserver
I know that there are a lot of shell scripts to do daily scan
But unfortunately I can't understand its lines :)
And I want to create a simple bash script to scan the /home directory and send me an email if there are infected files
#!/bin/bash
var=$(clamscan -i /home &> /dev/null)
if [[ $var != *Infected files: 0* ]]
then
echo "Subject: There are infected files" | sendmail -v root
fi
But the previous code doesn't work good
note : the problem in the code not in the file permission
UPDATE
The final worked code
#!/bin/bash
scanoutput=$(clamscan -ri /home 2>&1)
if [ $? -gt 0 ]; then
echo -e "Subject: ClamScan: there are infected files\nTo: root\n\n$scanoutput" | /usr/sbin/sendmail -t
fi
Try to use exit code instead of using the string output.
In the clamscan manual there is the follow
RETURN CODES
0 : No virus found.
1 : Virus(es) found.
2 : Some error(s) occured.
So you can try something like:
#!/bin/bash
clamscan -i /home &> /dev/null
clamscan_exit_code="$?"
if [[ "${clamscan_exit_code}" == '1' ]]; then
echo "Subject: There are infected files" | sendmail -v root
fi
Also you have the possibility to send a specific email in case where there is some error by checking exit code == 2.
You have to quote or escape spaces in the string you want to compare to:
[[ $var != *"Infected files: 0"* ]]
This [ answer ] suggests another approach to solving your problem, but I slightly modify it to the below form
#!/bin/bash
clamscan --quiet /home 2>/dev/null || echo "System infected" | sendmail -v root
PS: Fixed wrong logical expression
I'm trying to write a script to send my music from my computer to my android phone via ssh but I'm having some difficulties. Here is the piece of code :
while read SONG
do
echo $SONG
ssh -qi ~/.ssh/key root#$ip [[ -f "/sdcard/Music/${SONG}" ]] && echo "File exists" || echo "File does not exist"
done < ~/.mpd/playlists/Blues.m3u
The goal is to check if the song is already in the phone, and if it's not I'll scp it (if it's there it is with the same relative path than in the .m3u file).
I always got sh: syntax error: 'Davis/Lost' unexpected operator/operand and I think it is because there is a space before Davis that I can't escape
(the first $SONG is Geater Davis/Lost Soul Man/A Sad Shade Of Blue.mp3)
I also tried this, same result
while read SONG
do
echo $SONG
SONG="/sdcard/Music/$SONG"
echo $SONG
ssh -qi ~/.ssh/key root#$1 [[ -f "$SONG" ]] && echo "File exists" || echo "File does not exist"
done < ~/.mpd/playlists/Blues.m3u
Ideas welcome !!!
ssh command accepts only one argument as an command, as described in synopsis of manual page:
SYNOPSIS
ssh [...] [user#]hostname [command]
You need to adhere with that if you want correct results. Good start is to put whole command into the quotes (and escape any inner quotes), like this:
ssh -qi ~/.ssh/key root#$1 "[[ -f \"$SONG\" ]] && echo \"File exists\" || echo \"File does not exist\""
It should solve your issue.
I have done a lot of searching and I can't seem to find out how to do this using a shell script. Basically, I am copying files down from remote servers and I want to do something else if it doesn't exist. I have an array below, but I tried to reference it directly, but it is still returning false.
I am brand new at this, so please be kind :)
declare -a array1=('user1#user1.user.com');
for i in "${array1[#]}"
do
if [ -f "$i:/home/user/directory/file" ];
then
do stuff
else
Do other stuff
fi
done
Try this:
ssh -q $HOST [[ -f $i:/home/user/directory/file ]] && echo "File exists" || echo "File does not exist";
or like this:
if ssh $HOST stat $FILE_PATH \> /dev/null 2\>\&1
then
echo "File exists"
else
echo "File not exist"
fi
Assuming you are using scp and ssh for remote connections something like this should do what you want.
declare -a array1=('user1#user1.user.com');
for i in "${array1[#]}"; do
if ssh -q "$i" "test -f /home/user/directory/file"; then
scp "$i:/home/user/directory/file" /local/path
else
echo 'Could not access remote file.'
fi
done
Alternatively, if you don't necessarily need to care about the difference between the remote file not existing and other possible scp errors then the following would work.
declare -a array1=('user1#user1.user.com');
for i in "${array1[#]}"; do
if ! scp "$i:/home/user/directory/file" /local/path; then
echo 'Remote file did not exist.'
fi
done
I'm writing a reasonably lengthy script (or what I would consider lengthy - you could probably do it in a few hours). I basically have a file (named .restore.info) which contains files of names. In part of the script, I want to test "If cannot find filename in .restore.info, then says “Error: restored file does not exist”. Apologies if this doesn't make sense for you guys (for me, it does in the grand scheme of things). So if type this in the command line:
sh MYSCRIPT filename
It will search for the string filename in the .restore.info file, and if it cant find anything, it should produce an error message.
Basically, I need the top line of this coded translated into a UNIX bash statement and something that actually makes sense!:
If grep $1 .restore.info returns an exist status of 1; then
echo “Filename does not exist!”
fi
Thanks in advance! Please ask me if you need me to clarify anything more clearly as I know I'm not the best explainer, and I'll get back to you in less than a minute! (+rep and best answer of course will be given!)
You probably only care if grep exits with a non-zero exit status:
if ! grep -q "$1" .restore.info; then
echo "Filename does not exist!"
fi
but if you really do care about a specific exit status (1, in this case):
if ! grep -q "$1" .restore.info && [[ $? -eq 1 ]]; then
echo "Filename does not exist!"
fi
Use grep -q
grep -q "filename" .restore.info && echo "found match"
or
! grep -q "filename" .restore.info && echo "not found"
grep -l 'filename' .restore.info
if [ $? = 0 ];then
echo "found it"
else
echo "not found"
fi
I am writing a simple script to check if an entered directory path exists. This is what I have
echo "Please specify complete directory path"
read file_path
for file in $file_path; do
if [[ -d "$file" ]]; then
echo "$file is a directory"
break
else
echo "$file is not a directory, please try again."
fi
done
What I need is if it is not a directory to go back and ask for the file path again.
Thanks.
How about this?
echo "Please specify complete directory path"
while read file; do
if [[ -d "$file" ]]; then
echo "$file is a directory"
break
fi
echo "$file is not a directory, please try again."
done
No need to split the path into its parts, testing the entire path with -d will tell you whether or not it is a directory. You need to put the entire test into a while loop until the user gets it right:
#/bin/sh
set -e
file_path=''
while [ ! -d "$file_path" ]
do
echo "Please specify complete directory path"
read file_path
if [ ! -d "$file_path" ]
then
echo "$file_path is not a directory, please try again."
fi
done
I can use it like this
$ sh /tmp/test.sh
Please specify complete directory path
foobar
foobar is not a directory, please try again.
Please specify complete directory path
/var/www