[-d: command not found [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 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

Related

Bash If statement adding blank lines to end of document [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 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

Bash quotes 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 3 years ago.
Improve this question
I copied and running the command on my ubuntu 18.04 from here
https://kite.com/linux/ but got an error like:
$ bash -c “$(wget -q -O – https://linux.kite.com/dls/linux/current)”
bash: “”: command not found
$ type quote
quote is a function
quote ()
{
local quoted=${1//\'/\'\\\'\'};
printf "'%s'" "$quoted"
}
Any suggestions of the error?
You probably copied and pasted that from some word processor or website that turned the straight, regular, ASCII quotes " into pretty, curly Unicode quotes “ ”. Bash doesn't understand those. Just type them in by hand to fix the problem.

Using basic sed in bash script [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 4 years ago.
Improve this question
I'm sure this is quite simple. However, it's just not working for me. What stupid thing am I doing wrong please? I am running the following shell script like this:
bash test1.sh
Here's the code:
#!/bin/bash
bluesman_a="Magic Slim"
bluesman_b=($echo "$bluesman_a" | sed "/s/Slim/Sam/")
echo $bluesman_b
I get:
syntax error near unexpected token `|'
Thanks for your time
You need to use "$(...)" to wrap a command to assign the output to a variable and you need to remove the first / in the sed replacement command. Also, you do not need to use echo to pass a variable to sed.
bluesman_b="$(sed 's/Slim/Sam/' <<< "$bluesman_a")"
Or, to replace Slim with Sam just once, use
bluesman_b="${bluesman_a/Slim/Sam}"
See 10.1. Manipulating Strings.
See the online Bash demo

Bash - wait for a process (here gcc) [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 7 years ago.
Improve this question
On my Ubuntu machine I want to create a custom command for compiling a c file.
At the moment I have something liks this which does not work like I want to:
#compile the file
gcc $1 -o ~/.compile-c-output
#run the program
./~/.compile-c-output
#delete the output file
rm ~/.compile-c-output
The problem is that the run command is executed before gcc is ready and so the file does not exist. How can I wait until gcc is ready and I can run the file normaly?
Btw how can I add a random number to the output file so this script also works if I run it on two different terminals?
./~/.compile-c-output
Get rid of the leading ./. That's why the file doesn't exist.
~/.compile-c-output
To get a random file name, use mktemp. mktemp guarantees not to overwrite existing files.
file=$(mktemp) # unspecified file name in /tmp
gcc "$1" "$file" && "$file"
rm "$file"

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 (!).

Resources