What does `exec <&-` do? [duplicate] - bash

This question already has answers here:
What are the uses of the exec command in shell scripts? [closed]
(2 answers)
Closed 3 years ago.
Just editing a bash script and the last line is:
exec <&-
Hoping someone can explain. I'm guessing it has to do with the exit code of the previous command?

exec <&- closes standard input (filedescriptor 0).
exec with just redirections but not other argument applies the redirections to the current process.
A redirection to or from &- means close.
With numerical descriptors it doesn't matter if you do 0<&- or 0>&- — either version will close filedescriptor 0 (standard input). If you ommit the number > redirection means "use fildescriptor 1and<` means "use filedescriptor 0".

Related

why shell variable is not assigned to be the error messages from command used in command substitution? [duplicate]

This question already has answers here:
Output not captured in bash variable [duplicate]
(1 answer)
How to store standard error in a variable
(20 answers)
Closed 2 years ago.
I have the following:
a=$(grep pattern file_not_exist)
echo $a. #turns out a is empty
But I can see the grep complaining: grep: file_not_exist: No such file or directory.
Why is the error messages from grep not assigned to be the value of shell variable a? And if we want this kind of redirection, how to do it?
I am a shell green hand and just started. It seems stdout output are assigned to the shell variable. Could you point me to the documentation describing this kindly?
Thanks!
a will contain the output of the command returned to stdout. The error will be returned to sterr and so to get the error in the variable, you will need to redirect sterr to stout and so:
a=$(grep pattern file_not_exist 2>&1)
Here, 2 represent stdrr and 1 stdout.

Bash, assigning command output to a variable [duplicate]

This question already has an answer here:
Bash how do you capture stderr to a variable? [duplicate]
(1 answer)
Closed 3 years ago.
I am trying to automate the installation of nginx on multiple servers, and I have a shell script. It runs a version check if nginx is already installed and its version.
Trying to assign TMP=$(nginx -v), and instead of storing it in the variable it prints the results to console. printf "$TMP" prints an empty string
The problem is that your command does not print to STDOUT but to STDERR.
Using:
TMP=$(nginx -v 2>&1)
will solve your issue, see here for more details.

bash script, execute command line and keep going the next for loop [duplicate]

This question already has answers here:
Loop background job
(3 answers)
Closed 4 years ago.
Is it possible to run a command in a for loop without waiting for that command ended, while keeping going to the next iteration?
Because I have to send multi-files at the same time asap via many ssh connections, therefore I couldn't wait until the command ended one by one.
Maybe is it related to something like 'xterm' or 'gnome-terminal'?
Yes, you can execute the command in background by adding & at its end.
So the syntax looks like programName [arguments] & (at least for bourne compatible shells)

How to exit bash mode on server [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Exit Bash Mode?
Every time I enter my server I am now in Bash mode.
I don't want to be in bash mode because in it I don't have my current path on my left.
This question has already been asked here:
Exit Bash Mode?
but OP accepted an answer that isn't really an answer.
I have already tried 'reset' and removing my .bashrc file.
Try "chsh -s /bin/sh" if you want to use sh. To see what your available shells are, "cat /etc/shells". You can also see what your current shell is by "echo $0" and simply "exec /bin/sh" will change shell for your session (not perm).
Changing default shell - /etc/passwd - for your unix account:
username:x:1000:1000:,,,:/home/username:/bin/bash
^^^^^^^^^

What does "2>&1" here mean? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In the bash shell, what is “ 2>&1 ”?
#echo "[+] Creating $count files"
_remount
create=$((time ./dirtest $testdir $count $size) 2>&1)
It means that the standard error descriptor stderr (2) is to be redirected to the standard output descriptor stdout (1). Notice that the & ensures that the 1 is interpreted as a file descriptor and not a filename.
It redirects standard error to the standard output. This way the result written to stderr will be stored in the variable.

Resources