How to hide the command when using command repetition with the exclamation mark? [closed] - linux

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
When I use ! to execute a previous command it automatically prints the command as well. Is there a way to hide this?
Example:
This is what happens:
ralgrad:~$ echo test
test
ralgrad:~$ !!
echo test
test
This is what I would want:
ralgrad:~$ echo test
test
ralgrad:~$ !!
test

I have looked at the bash source and there is no way to disable this automatic printing of the expanded command. You would have to compile your own version of bash!
If it is particularly important to you for whatever reason, look in bashhist.c in the pre_process_line function and comment out/remove the following line:
printf (stderr, "%s\n", history_value);

You cannot do that with !!, because it repeats the last command not the last output. As far as I know, there is no single command that allows to achieve what you are asking. However, you can try a simple hack:
result=`echo test`
echo "$result"

Well, you can simulate what you want with the following batch file:
if [ -z $1 ]; then
exe=`fc -lrn | awk 'NR==2 {print;}'`
else
exe=`fc -lrn | cut -f2- | awk 'NR>=2 {print;}' | grep "^ $*" | head -n1 `
fi
eval $exe
Let h be the name of the file (or, if you want it, even !). Then you can do:
# echo Foo
Foo
# echo Bar
Bar
# h
Bar
# h echo F
Foo

Related

Variable quoting difference between bash 4.3.43 and 4.4.19 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
Out of curiosity I wonder which change in bash (or head?) made this the following behaviour change,
In 4.4.19 we have the following behaviour,
# Assign "foo <new line> bar" to variabe 'var'
$ > var="foo
bar"
# Echo with and without quotes,
$ > echo "${var}"
foo
bar
$ > echo ${var}
foo bar
# Read all lines (1) except the last,
$ > head -1 <<< ${var}
foo
$ > head -1 <<< "${var}"
foo
Doing the exact same thing in bash 4.2.46 and 4.3.43 results in different output when reading the variable with head.
# Assign "foo <new line> bar" to variabe 'var'
$ > var="foo
bar"
# Echo with and without quotes,
$ > echo "${var}"
foo
bar
$ > echo ${var}
foo bar
$ > head -1 <<< ${var}
foo bar
$ > head -1 <<< "${var}"
foo
So it seems to me (with 4.4.19) that no matter if you quote the variable or not, head's input will be both lines. And with versions 4.2.46 and 4.3.43 the input actually differs depending on if you quote the variable or not.
The earlier behaviour makes sense to me, where you would have to quote the variable if you want the new line. I'm genuinely interested in this behaviour change and the reasoning behind it. I tried looking through the bash-changelog, but nothing obvious stood out to me that would lead to this change (although I have some very vague feeling that I've stumbled on this before).
Thanks in advance!
The behavior in bash 4.3 is a bug which is fixed in 4.4. See the original bug report and
Chet's reply (2015/09).
And the most recent posts to bash mailing list regarding this: the question and Chet's reply (2017/11).

How to color ls - l command's columns [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I wonder if possible to have ls -l colored. I'm not talking about --color, of course.
I found an useful alias for display octal permission in an ls -l command, now, it's possible to color it? In the same way, is possible when I do ls -l, display only permissions in red or something?
I don't know how to use color code, but grep has --color option
If the first line of ls -l is not important to you, you can consider using grep
ls -l | grep --color=always '[d-][r-][w-][x-][r-][w-][x-][r-][w-][x-]'
or in shorter form:
ls -l | grep --color=always '[d-]\([r-][w-][x-]\)\{3\}'
You can use several utilities to do it, like piping the output of ls (OPTIONS...) to supercat (after defiining the rules). Or to highlight (after defining the rules).
Or use awk/sed to pretty print based on regexes. E.g. with gensub in awk, you can insert ANSI color codes to the output...
The first thing that came into my mind is that you can use --color=auto for this:
ls -l --color=auto
And it can be handy to create an alias:
alias lls='ls -l --color=auto'
However I see you don't want that. For that we have to create a more complex function that use the echo -e "colours...":
print_line () {
red='\e[0;31m'
endColor='\e[0m'
first=${1%% *}
rest=${1#* }
echo -e "${red}$first${endColor} $rest"
}
lls () {
IFS=$'\n'; while read line;
do
# echo "$line"
print_line $line
done <<< "$(find $1 -maxdepth 1 -printf '%M %p\n')"
}
If you store them in ~/.bashrc and source it (. ~/.bashrc) then whenever you do lls /some/path it will execute these functions.
If you're asking if there is an option to specify custom column-specific colors in ls, I don't think so. But you can do something like.
> red() { red='\e[0;31m'; echo -ne "${red}$1 "; tput sgr0; echo "${*:2}"; }
> while read -r line; do red $line; done < <(ls -l)

How can i took specific word from line basic in linux [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 9 years ago.
Improve this question
Suppose i have one line Script :
Script Name is script1.sh has below line on it -
# sh script.sh #
So how can i take only script.sh name from script1.sh.
What I have done is below but that is not fully fruitful to me get the exact output that I want.
while read line
do
called_script= awk -F ':' '{print $1 }' final_calling_script_name
qwe= grep '*.sh' $called_script
echo $called_script " : $qwe"
done<'file_that_contains_data_of_script1_line_by_line'
Can anybody help me?
If what you want here is basically "the second word" you can use "cut"
echo "sh script.sh" | cut -d ' ' -f 2
The -d ' ' tells cut that the "delimiting character" is a space, the -f 2 tells cut that you want column number 2.
echo "sh script.sh" | { read a b; echo "$b"; }
EDIT:
After you've clarified your requirements in the notes below, I would propose this command:
echo "script1.ksh script2.pig script3.sh" | grep -oe '\w*\.sh'

Bash script manipulation [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 8 years ago.
Improve this question
I work with Bash script and I want to get line from big text by special text
for example i have these lines
first fffffffffffffffffffffffffff
.................................
second ssssssssssssssssssssssssss
.................................
third ttttttttttttttttttttttttttt
and I want to get ssssssssssssssssssssssssss string .
Can anybody help me?
Is this what you want?
echo "$longstring" | awk '$1 == "second" { print $2 }'
since you seem to not have any criterion as to which line you want to output, i suggest something like:
echo "ssssssssssssssssssssssssss"
this is pretty robust regarding the content of your input, doesn't depend on a "file", and is a fast solution.
cat filename | grep "^second" | cut -d " " -f 2
Or, if you are ALF:
<filename grep "^second" | cut -d " " -f 2
Or
grep "^second" filename | cut -d " " -f 2

Grep script to run on CD [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Im trying to learn some bash script writing and cleaning out some old CDs (remember those?)
What I have is a bunch of backup CDs with no rhyme or reason to them so I'd like to create a grep script that will search them based on keywords. (I'm currently trying to test the script first against the desktop) I know how to run grep, but its the scripting I'm having trouble with.
So here's what I have:
#!/bin/bash
#CDGrepScript
SOURCEDIR=/home/name/Desktop (I'm currently testing it with files on the desktop)
#mount /dev/cdrom
#SOURCEDIR=/dev/cdrom
echo "hello, $USER. I will search your files"
echo Begin Search...
grep -ir "taxes|personal|School" *
echo $results
echo "The search is complete. Goodbye"
exit
Now when I run this against files on the desktop. My script hangs after "Begin search" What am I doing wrong?
Thanks for the help
You might be served better by a more general tool. Like a rgrep (recursive grep) that will walk through a tree searching for a search term. An example:
# rgrep
#
# Search for text strings in a directory hierarchy
set +x
case $# in
0 | 1 )
# Not enough arguments -- give help message
echo "Usage: $0 search_text pathname..." >&2
exit 1
;;
* )
# Use the first argument as a search string
search_text=$1
shift
# Use the remaining argument(s) as path name(s)
find "$#" -type f -print |
while read pathname
do
egrep -i "$search_text" $pathname /dev/null
done
;;
esac
Put this in your path, then you just change directories to the mount point for the CD-ROM, and type
$ rgrep "taxes" .
Or whatever other search you wish to perform.

Resources