Bash's $_ variable not expanding [closed] - bash

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
The $_ variable, which should expand with the arguments of the last executed command in Bash, contains _lp_runtime_before. No matter how many times I run a command, that would be the content it has. Why?

Actually, $_ expand to the last argument of the last command line, according to the man page of bash:
[$_] expands to the last argument to the previous command, after expansion.
If you want the whole arguments, use !:*:
$ ls -a -l -h test
[blah blah]
$ last_command="!:*" > /dev/null
$ echo $last_command
-a -l -h test
I added a redirection of stdout to null device to prevent bash to print the expansion.

Related

Why won't a value in this bash script equal a command? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
When I run this code, the variable "thing" doesn't change its value to the command. I've tried everything and I just can't get it to work. I want thing to equal something like "1 history cd /bin
history cd /home/user/"
#!/bin/bash
val="thing"
function send () {
thing
thing=$(history | tail -n 2)
echo $thing
echo $val
# echo $last
if [ "$val" == *"this"* ]; then
echo "yes"
fi
exit 1
}
send
If you wonder why $(history | tail -n 2) returns nothing, it is because history lists commands previously ran in the current shell.
But your script is a new shell instance, so it does not carry the history of commands you ran before you execute your script.
If you want that, you have to source the script, not execute it. To source, do:
$ . thescript.bash
instead of
$ ./thescripts.bash
instead of this also
$ bash thescript.bash
Note: put your code in https://www.shellcheck.net/ to see syntax issues.

I'm unable to spot the errors in this 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 2 years ago.
Improve this question
I have the following script
#./bin/bash
#Check Error Script
echo "Try to find out some errors!!!"
# Search for the words which can be matched by regex [^a]*ce
echo "The regex [^a]*ce can match the string(s):" > Result
# And append matched strings to file "Result"
cat <<START | grep '[^a]*ce$' > Result
lance
ace
brace
decide
piece
-END
# The append the output of command simple.sh to file "Result"
.simple.sh>>Result
echo "Congratulations! You have corrected all the errors!"
And I'm trying to get it to say
Try to find out some errors!!! Congratulations! You have corrected all
the errors!
but it keeps showing
Try to find out some errors!!! ./checkError.sh: line 21: warning: here-document at line 10
delimited by end-of-file (wanted `START')
Your /* Check Error Script */ comment is interpreted as globbing (do ls -ld /* and you'll see), matching /1 which it tries to execute.
Use # for comments in bash.
You also have a here-document that you start with START but you try to end it with -END. Use END in both places.
Also, cat <<START | grep '[^a]*ce$' > Result should most probably be cat <<END | grep '[^a]*ce$' >> Result. Note the >> to append before Result.
Something like this:
#./bin/bash
# Check Error Script
echo "Try to find out some errors!!!"
# Search for the words which can be matched by regex [^a]*ce
echo "The regex [^a]*ce can match the string(s):" > Result
# And append matched strings to file "Result"
cat <<END | grep '[^a]*ce$' >> Result
lance
ace
brace
decide
piece
END
# The append the output of command simple.sh to file "Result"
.simple.sh>>Result
echo "Congratulations! You have corrected all the errors!"
Note: This requires the script .simple.sh to be in your PATH. The comment above it suggests that the script's name is simple.sh and not .simple.sh, so if .simple.sh doesn't exist, I'd try simple.sh or ./simple.sh.

Sed command not executing correctly [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 new to Bash and trying to modify a file using sed. Consider this simple example:
#!/bin/bash
shopt -s extglob
file="/root/test.txt"
# Modify the file.
sed "s/apple/orange/g" $file -> $file.tmp && mv $file.tmp $file
I run the script, and it hangs. Looking at the directory:
ls
test.sh test.txt test.txt.tmp
Shows test.txt.tmp is created, but mv is not working.
What is wrong with the script?
The problem is in the following line:
sed "s/apple/orange/g" $file -> $file.tmp && mv $file.tmp $file
Bash interprets this line as:
sed "s/apple/orange/g" $file - > $file.tmp && mv $file.tmp $file
Notice the subtle difference (the space between - and >).
Now, what is happening? The - <hyphen> is interpreted to be a file. More specifically /dev/stdin. This implies that sed is awaiting input from /dev/stdin. That is also the reason why it is hanging. It is actually not hanging but awaiting input. The file $file.tmp is created, but since the sed command is still executing, the mv is not happening.
The standard input shall be used if no file operands are specified, and shall be used if a file operand is - and the implementation treats the - as meaning standard input. Otherwise, the standard input shall not be used. See the INPUT FILES section.
source: POSIX sed standard
Also related: Usage of a dash in place of a filename

How to grep the pidof bash in a shell script? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to get the PID of bash and highlight it if it's in some textfile (assume it is). So when I'm typing this in my shell:
grep -o $(pidof bash) test.txt
it just works fine and gives me the desired output, the PID of bash.
Then why is this script not working:
#!/bin/bash
PID=$(grep -o $(pidof bash) test.txt)
echo $PID
I only get some lines with:
grep: xxx: file or directory not found
xxx are random numbers, but usually the last one is the one I'm looking for.
How do I achieve this and why is the above not working?
Has this something to do with creating a new process by the shell when calling grep in the script?
Thank you.
I don't have pidof, so I'm assuming that it's an equivalent to pgrep -v, printing a list of PIDs, one on each line, with a newline between them.
If that's so, consider this:
egrep -o "$(pgrep -v bash | tr '\n' '|')" test.txt
Assume that the output of pgrep -v bash is:
123
456
789
Your original code would do this:
egrep -o 123 456 789 test.txt
...thus, searching for 123 in a file named 456, in a file named 789, and in a file named test.txt.
Now, compare to what happens when you replace that whitespace with pipe symbols:
egrep -o "123|456|789" test.txt
...as executed by the pipeline suggested earlier in this question is exactly what you were looking for. (BTW, the quotes here are purely syntactic -- that is, they're for consumption by the shell when it's understanding how things are parsed, not passed to egrep).
That said, if you're looking for the current bash process, use either $$ (for the parent PID of the current shell) or $BASH_PID (for the current shell itself even if it's a subshell), rather than using so inexact a tool as pgrep or pidof.
When you run pidof inside your shell script, there are at least two instances of bash running, so it will return multiple numbers. The way grep is designed, it can only search for one pattern at a time so the first number is interpreted as a pattern and the other numbers are mistakenly interpreted as file or directory names.
I'm not sure which bash PID you care about, but one solution is to use something like grep, head, or tail to filter the output of pidof so you just get one number. Another solution is to use the special variable $$, which is the PID of bash instance that evaluates it.
In the future, you can debug this better for yourself. To debug, I would start by running this command inside your script to see exactly what arguments are being passed to grep:
echo grep -o $(pidof bash) test.txt

Unix Shell scripting query on sed [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Need a help on unix shell scripting. As I am new to this.
I have a shell script written. While running that script am giving argument as say 003. I need to replace this argument value in a particular line in shell scripts as below.
Script:
if [[ $# != 1 ]]
then
echo "Please enter the Value"
echo "eg: script.sh 003"
exit 0;
fi
Q=WMS.XXX.vinoth
I need to replace XXX value with 003 and append into a temp file. Can you please help me???
Thanks in advance!!!
Do you ask how to replace the XXX with first argument?
Q=WMS.$1.vinoth
Using the $1 will work, however be aware that you should probably use quotation marks when passing a number like 003 in stead of 3 as in some cases the two zeros in front might be dropped.
Also I recommend wrapping the string in quotation marks as well, avoiding accidental command calls.
./script "003"
if [[ $# != 1 ]]
then
echo "Please enter the Value"
echo "eg: script.sh 003"
exit 0;
fi
Q="WMS.$1.vinoth"

Resources