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"
Related
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 test the script in WSL2
Here is my script:
#!/usr/bin/bash
echo "testing..."
printf "testing..."
It work fine if I run like
bash test
source test
. test
But it output nothing if I add the path the script located in to PATH and run
test
Why and how can I fix it?
test is a bash built-in. POSIX systems will also have a test executable.
When you enter a command without specifying a path to the executable, bash will first check if the command is one of its built-in commands before searching for the executable in the PATH. If the command matches the name of one of the bash built-ins, it will run the built-in.
If you still want to run your script without specifying its path, there are two ways to do it:
Recommended: Rename your file, and then run it with its new name (your script file needs to have its executable permission bit(s) set).
Make sure your script has its file permissions set so that it is executable, make sure your PATH is set up so that your test will be found before the system's test, and then run env test to run your script. env will search your PATH to find your test executable, and then it will execute it.
Ultimately, option 2 is not recommended, because it can be brittle to reorder your PATH, and it can be confusing (for you and for others) to have a second test binary on your system.
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 have a file called data. Although the file doesn't have an extension, I used the file bash command to see what type of file it is:
$ file data
data: POSIX tar archive (GNU)
Seeing as it is a tar file, I try to open it but I get an error:
$tar xvf data
tar: data: Not found in archive
You need to add a hyphen before the xvf.
tar -xvf data
Else, if you are in another location.
tar -xvf /<path to data>/data
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
I am not sure when this behavior started but by typing the ls command, I am getting the following output:
$ls
./ ../ .DS_Store Books/
I am not very sure about the first three items and they always come in every folder. Can anyone explain me how to get rid of them? I am using OS X Yosemite
The first two: ./ and ../ are current directory and parent directory respectively. You can't get rid of them. The last one .DS_Store is probably some config file/directory which you can remove with:
rm -f .DS_Store # use -r if it's a directory
But be sure to check what's its for!
The bahaviour of ls is not the reason for the "extra" output. You probably have an alias something like:
alias ls='ls -a'
in your shell. To find out exactly what it's aliased to, do:
type ls
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
I want my bash script to copy the files inside the foo directory into the baz directory.
When I run this command in the terminal, it achieves what I expect:
cp -r /foo/. /baz
But when I save it as a bash script:
#!/bin/bash
cp -r /foo/. /baz
And run:
./script.sh
Then it unexpectedly copies the foo directory itself into baz (rather than only the files in foo).
What am I doing wrong? Why is this happening? How do I fix the bash script?
Edit - bad question. I ran an old version of my script without noticing. Everything does work as expected. The answers still helped me with alternative solutions.
Use rsync instead. It doesn't copy the parent directory:
rsync -r /foo /baz
Change the content of the script to:
#!/bin/bash
cp -r /foo/* /baz
To be honest, I'm not sure why you run into this issue. It works fine for me. Still, the asterisk seems more appropriate. Which OS are you running?
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