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.
Related
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
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
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.
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
I'm trying to remove all lines of text that contain a double quote, and I have tried this:
sed -ne '/\"/!p' theinput > theproduct
It left the lines untouched. What do I do? Here is my script:
`touch tmp.txt
open tmp.txt
read -sn 1 -p "Paste in data and press any key to convert"
echo
touch tmp.txt
open tmp.txt
read -sn 1 -p "Paste in data and press any key to convert"
echo
sed -e 's/-/ /g' tmp.txt > tmp2.txt
grep -v '"' tmp2.txt > final.txt
open final.txt
echo Study Conversion Successful
The first sed command works. It replaces a hyphen with a bunch of spaces (don't ask why I need that). The grep command, which I added from a response, does not work. It leaves the lines with quotes untouched.
Its not necessary to escape the double quote:
sed -ne '/"/!p' theinput > theproduct
Very strange. It "works for me"
$ cat data.txt
dsklfljs
sdjflk"Sdgsd"
sdfj sldkfj "Sdfsd"
sdfj
sdf
sdjflks
$ sed -ne '/\"/!p' data.txt
dsklfljs
sdfj
sdf
sdjflks
Perhaps it is a version issue with sed?
However, you can also consider using grep -v for this.
$ grep -v '"' data.txt
dsklfljs
sdfj
sdf
sdjflks