Check whether two files exist (in shell script) - file

I'm writing a shell script and trying to check whether two files exists.
Here is the script example:
#!/bin/bash
if [[ [ -e File1Name ] -a [ -e File2Name ] ]]
then
echo Yes
el
echo No
fi
and get
script: line 5: conditional binary operator expected
script: line 5: syntax error near `-e'
script: line 5: `if [[ [ -e CA ] -a [ -e CA-draw ] ]]'
What is wrong with my script and hot to fix it?

if [ -e File1Name -a -e File2Name ]
then
echo Yes
else
echo No
fi

Both [[ and [ are commands; you need to pick one of them, and use only it with if.

Related

while cycle not stopping in bash

My script is designed to get files (and folders, eventually) and then list them with a while iteration.
format should be:
1) file.txt
2) newfile.txt
3) new folder
and it works! but then the while cycle wouldn't stop. given that i'm a newbie in bash can you tell me what am i doing wrong?
#!/bin/bash
shopt -s nullglob
FILES=(*)
var2="0"
while [ ${FILES[var2] -n } ] ; do
echo "$var2 ${FILES[var2]}"
((var2++))
done
The syntax is [ -n "string to check" ]:
#!/bin/bash
shopt -s nullglob
FILES=(*)
var2="0"
while [ -n "${FILES[var2]}" ] ; do
echo "$var2 ${FILES[var2]}"
((var2++))
done
You can also just do:
#!/bin/bash
shopt -s nullglob
num=0
for file in *
do
echo "$num) $file"
(( num++ ))
done
If this is the basis for a selection menu, you can instead use a select loop which would be much simpler.

exiting a shell script from a c program compiled from the same script

I've written the following shell script to compile and execute a c program and then to do some other operations.
#!/bin/sh
#
#FIRST SCRIPT
#
clear
echo "-----STARTING COMPILATION-----"
#echo $1
name=$1
#echo $name
find . -iname $name -maxdepth 1 -exec cp {} $name \;
new_file="tempwithfile.adb"
cp $name $new_file
cp $name1 $name
echo "compiling"
dir >filelist.txt
gcc writefile.c
run_file="run_file.txt"
echo $name > $run_file
./a.out
echo ""
echo "cleaning"
echo ""
make clean
make -f makefile
Can I stop the execution of the shell script if a particular condition is met in the c program? For e.g, if I am searching for a file from the program and i didn't find it, then I don't have to execute the rest of the shell script. Is it possible?
As ManĂ¼l said, you need to return a value from the main() function in the C code.
Then you can do this in the script:
./a.out
if [ $? -ne 0 ]
...

Running a C Program in Bash Script

When I run a C program in this bash script it returns the error.
ssh -n -f *.*.*.* "cd /home/sth/remote && echo "$1" && det=$(./ossec-rootcheck)">/home/sthh/res
Error:
./ossec-rootcheck: No such file or directory
I want to ssh to a remote machine and then run a program on it. I know that this file is located in that path because when I edit it as you see, it works.
ssh -n -f *.*.*.* "cd /home/sth/remote && echo "$1" && ./ossec-rootcheck">/home/sthh/res
and as it echo $1 I can see that it does cd /home/sth/remote right. But I want the return value of that program to be stored in a variable,for example det.
ssh -n -f *.*.*.* "cd /home/sth/remote; echo "$1"; ./ossec-rootcheck || do_your_work">/home/sthh/res
You don't have to store it in a variable.
|| executes do_your_work if the exit status of ossec-rootcheck != 0
If you want to store the numeric exit status in a variable, or echo it, you can do (with proper escaping):
./ossec-rootcheck; ecode=$?; echo $ecode
To get the return code or exit code of the remote code:
ssh -n -f *.*.*.* "cd /***/***/remote && echo \"$1\"; ./ossec-rootcheck; echo \$?">/home/ossl7/res
To capture errors as well:
ssh -n -f *.*.*.* "exec 2>&1; cd /***/***/remote && echo \"$1\"; ./ossec-rootcheck; echo \$?">/home/ossl7/res
Also, you probably need to omit && echo \"$1\" when you find it to be working already. And you could just use single quotes for that:
ssh -n -f *.*.*.* 'cd /***/***/remote; ./ossec-rootcheck; echo $?' >/home/ossl7/res
Or
ssh -n -f *.*.*.* 'exec 2>&1; cd /***/***/remote; ./ossec-rootcheck; echo $?' >/home/ossl7/res

Using wgets to connect to sites in a txt file using shell scripting

I am trying to create a script that uses wgets to access every url from a list in a file. However when doing this instead of accessing website.com it will try to connect to website.com/r/n. I know this has to do with text formatting in the text editor but I'm unsure of how to get my program to ignore this. This is the code I have:
#!/bin/bash
for i in `cat $1`
do
wget --spider $i
if wget --spider $i 2>&1 | grep --quiet "200 OK" ; then
echo $i >> connected.txt
else
echo $i >> unsuccesful.txt
fi
rm wget-log
done
Add this to the top of your script
dos2unix "$1"
IHTH

How do I sort a directory full of pictures into sub-directories based on the image name?

I want to take all of the files in /media/mdrive/dump/:
1COD-234355.jpg
MAK-LXT218.jpg
ZIR-CON145.jpg
And create and sort them into the following directories:
/media/mdrive/dump/1/1COD-234355.jpg
/media/mdrive/dump/M/MAK-LXT218.jpg
/media/mdrive/dump/Z/ZIR-CON145.jpg
How would I do that?
This script takes a directory as the first argument and performs what you need:
#!/bin/bash
DIR="$1"
if [ -z "$DIR" ]; then
echo >&2 "Syntax: $0 <directory>"
exit 1
fi
if [ ! -d "$DIR" ]; then
echo >&2 "\"$DIR\" is not a directory"
exit 1
fi
cd "$DIR"
for file in *.jpg *.JPG; do
first=${file::1}
mkdir -p $first && mv $file $first/;
done
head -c xx will return the first xx characters of its input (here, the filename). mkdir -p will skip directory creation if it already exists.
to make two directories you could try something like
dir "/media/mdrive/dump/1/" :: CD would also work here
mkdir folder 1
mkdir folder 2
from here I think you can continue with your IF statements and so forth.
all you need to do is set the dir commands with the Direct path takes the guess work out.
then to check each just do:
start explorer.exe "the folder's path here"
it should open the folder to view the files

Resources