Bash If statement adding blank lines to end of document [closed] - bash

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a simple script which checks if a file exists, it works fine but seems like every time It executes, adds a new blank line to the end of the document
if [[ -f /usr/local/sbin/.env ]];
then
:
else
touch /usr/local/sbin/.env
fi
It should only check if the file exists, If it does, do nothing => : else create it:

just check with negation
if [[ ! -f /usr/local/sbin/.env ]];
then
touch /usr/local/sbin/.env
fi

Related

Curl does not work when passes in variable from file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am not sure why using the variable inited from a file does not work.
I wrote a function to CURL a list of domains from a file (only one line at this time, www.google.com). But the CURL returns empty when the $domain is read from the file. When I echo $localvar, it always displays the correct value (www.google.com in this case).
I hardcoded "www.google.com" for a test, the CURL did return the page as expected.
What did I miss? Thanks in advance!
Bash version 3.2.57(1)-release
#!/bin/bash
function processDomains() {
local inputfilepath=$1;
while read domain
do
local localvar="$domain"
#localvar="www.google.com" --- uncomment this line the curl command works
#echo $localvar --- always display domain correctly
local result=$(curl -sL "$localvar");
echo "$domain" "$result"
done < $inputfilepath
}
Jonathan's comment answered my question. It was caused by the CRLF ending from the file.

Make perl exit with 1 if a string is found [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want a bash command (perl?) that will output a non-zero exit code if a non-ASCII character is found in a text file.
Here's what I have so far:
perl -nle 'print "$." if m/[\x80-\xFF]/' file_that_has_non_ascii_characters.txt
This prints out each line that a non-ASCII character is found on. I tried a variant with exit 1 in it, but it doesn't seem to work:
➜ perl -nle 'exit 1 if m/[\x80-\xFF]/' file_that_has_non_ascii_characters.txt
➜ echo $!
0
How do I do this?
You should be using $? instead of $!.
!: Expands to the process ID of the most recently executed background (asynchronous) command.
?: Expands to the exit status of the most recently executed foreground pipeline.

Bash: syntax error near unexpected token `else' not finding error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I must be missing something, or some kind of error that just isn't sticking out to me. I'm sort of new to Bash, and I'm learning on a program assigned to me by work. I keep getting that error.
if ! [[ $1 =~ $re ]]; then
echo "Argument is string"
echo "Running string function"
findProcessName()
else
echo "Argument is number"
echo "Running number function"
findProcessID()
fi
Answer: parenthesis are not used when calling functions.

Bash syntax error: line 7: unexpected end of file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
So I have a bash script
!#/bin/bash
while [ true ];do
ls -lah /sth/ | grep sth*
sleep 0.001
done
exit 0
I thoought thai it was ok but when I run it I get
line 7: syntax error: unexpected end of file
But the code has only 6 lines?
What may be a problem? I edited the file in linux, deleted unnecessary spaces but still my scropt doesn't work.
The shebang line is wrong. You are not running it under Bash at all.
#!/bin/bash
Notice the order of the sharp (#) and the bang (!).

[-d: command not found [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
As per this answer: Unix Bash Shell Programming if directory exists, I'm trying to check if a directory exists. However, when I run this, I get line 1: [-d: command not found. What am I doing wrong here?
if [-d "~/.ssl"]; then
echo '~/.ssl directory already exists'
else
sudo mkdir ~/.ssl/
fi
[-d
is not a command.
[ -d
is the test command with the -d option.
Space matters.
(Also, the [ command needs to end with a ] parameter, which likewise has to be separated from other arguments by whitespace.)
That's the crux of the matter. There is another issue, though: If you quote the tilde, it doesn't expand. (This is one of the rare place where you may want to avoid quotes.) Quotes are great, though, so why not write "$HOME/.ssl"? (There's a subtle difference between ~ and "$HOME", but it doesn't matter for most uses.)
Honestly, all you really need is probably:
if mkdir -p ~/.ssl; then
# Do stuff with new directory
else
# Handle failure (but keep in mind `mkdir` will have its own error output)
fi

Resources