Tag Info

New answers tagged

2

You were close, but you should use single quotes, not double quotes: kill_stopped='kill `jobs -p` ' Backticks are expanded inside double quotes, so it was running jobs -p at the time you defined the alias, not when you used it.


1

Try this: alias kill_stopped="kill \$(jobs -p)" and to kill runnings jobs: kill_stopped If there are no running jobs you get a message about the usage of the kill.


0

The two commands produce the same output only for your input but otherwise they are different. For understanding of what is going on we have to know how is the parameter interpreted first by bash and then by grep. Escaping in bash \ is a special character which cancels special meaning of the following character including \ itself. If the following ...


4

The output is the same only for your string, but in general those regular expressions do different things. Let's modify your example a little by adding second pattern e,g, (with comas), third e\.g\. (dots), fourth e\,g\, (comas), and -o option to grep to print only matched parts. In the following case . match any char (notice '' around e.g., I will come to ...


3

When you do a grep e\.g\., the shell is consuming the backslash, thus you are doing a grep e.g., which matches. When you do a grep e\\.g\\., the shell is again consuming a slash, and now you are doing a grep e\.\g., which again matches. Now, a backslash to the shell looks like \\. So, when you have \\, the first one is an escape sequence, the second is a ...


8

First, note that the single slash matches too much: $ echo $'eegg \n e.g.' | grep e\.g\. eegg e.g. As far as Bash is concerned, an escaped period is the same as a period. Bash passes on the period to grep. For grep, a period matches anything. Now, consider: $ echo $'eegg \n e.g.' | grep e\\.g\\. e.g. $ echo $'eegg \n e.g.' | grep e\\\.g\\\. e.g. $ ...


4

@artm showed a technique where you double-quote the awk script and escape various characters. Here are 3 other techniques Break out of the single quote to let the shell expand the variable usrpid=$(awk '$1 == "'"$USR"'" {print $2}' file) Pass the shell variable into an awk variable usrpid=$(awk -v usr="$USR" '$1 == usr {print $2}' file) If the ...


1

the "$USR" in the first example isn't expaneded because it occures inside single quoted string '$1 == "$USR" { print $2 }', so this code is looking for a row with the first column being "$USR", not 62. The following should work: usrpid=$(awk "\$1 == \"$USR\" {print \$2}" /home/hu/batchhu/dbscripts_tst2/user-pid.out2) Changes: the awk command line ...


1

Unquoted newlines in command lines get treated as spaces; that's why you can say things like command1 && command2 Well, sometimes unquoted newlines get treated like semicolons.  But, when you say echo `cat ${log}` all the newlines in the log file get turned into spaces.  You could fix this by saying echo "`cat ${log}`" but why not just say cat ...


10

A few pieces of documentation will help to explain this. From the POSIX standards document for the shell: The following variables shall affect the execution of the shell: PS1: Each time an interactive shell is ready to read a command, the value of this variable shall be subjected to parameter expansion and written to standard error. ... ...


4

The \ character escapes the following (special) character. In this case, it escapes the $, which we usually use to dereference a variable. When the shell evaluates a variable assignment, it first expands the right-hand-side of the expression. Without the \ before $PWD, the shell expands $PWD and assigns the result to PS1. However, with the \, the shell ...


2

You just need to escape the dollar $.: echo \$PATH $PATH Or surround it in single quotes: echo '$PATH' $PATH This will ensure the word is not interpreted by the shell.


4

When executing a command, the list of arguments is a list of pointers to NUL terminated strings passed to the execve() system call (just like the environment variables which is the other list of NUL-terminated strings passed to execve()). As a result, arguments and environment variables of executed commands cannot contain the NUL character. Exception to ...


6

If you are using bash, the easiest way to save a command exactly is to put it in an array. If the command you want to run is: example -options "-i filename" you can save it in an array variable with: commandline=(example -options "-i filename") Then, you can run the saved command exactly as is by @-expanding the array inside double-quotes: ...


1

What you wrote should work. It doesn't because you forgot a quote. It should be: if [ "`dmesg | grep "Firmware patch 1563"`" == "" ] Note the extra " after 1563. With that being said, I think the other answer gives you better ways to achieve what you want to do.


8

You can test the result of a command directly: if dmesg | grep -q "Firmware patch 1563" If you need to check whether the output of a pipe is empty, use $() for command substitution and nest your quotes as you would with a free-standing command: if [ -z "$(my_command | other_command "some argument")" ]


4

You can use command substitution directly: if $(dmesg | grep -q "Firmware patch 1563"); then # Do something here fi Or a better way, use commands directly like l0b0's answer.


1

If you do not wish to change your script you can also use xargs (considering you have it or it's available on your platform) by running it like this: ls *.txt | xargs -L1 ./remove_cc xargs is a utility that takes stdin and transforms it to command parameters on a command to be run. It has the -L flag that limits the amount of input lines to be used in a ...


5

When you type ./remove_cc *, the shell changes it to ./remove_cc file1.txt file2.txt file3.txt etc, and runs your script that way. Your script is only looking at $1, the first parameter (file1.txt). The most general way to do this is to loop over each parameter in turn. "$@" expands to the list of all the parameters, and we can use for to loop over them. ...


6

just use for x in * do ./remove_cc $x done By the way, you can combine all sed in one line sed -i \ -e 's/@r//g' -e 's/@g//g' -e 's/@y//g' -e 's/@b//g' \ -e 's/@m//g' -e 's/@c//g' -e 's/@n//g' -e 's/@R//g' \ -e 's/@G//g' -e 's/@Y//g' -e 's/@B//g' -e 's/@M//g' \ -e 's/@C//g' -e 's/@N//g' -e 's/@W//g' -e 's/@K//g' \ $1 you can ...


1

Use "$@" instead of $1. Or quote the wildcard: $ bash -c 'echo $1' 'some command' '*' file1 file2 $ bash -c 'echo "$@"' 'some command' * file1 file2


1

In double quotes, you need to backslash backslashes, i.e. double the backslash before the dot. system("grep '^.*\\.[a-zA-Z0-9][a-zA-Z0-9]*\$' file.txt > file2.txt"); # ^ # | # Here.


5

To make this an alias, which is possible, you need to use double quotes around the entire value for the alias. You'll also need to escape a few things within the alias as a result, and you need to escape any of the field arguments to awk since these will get interpreted as arguments by Bash as well when you're setting the alias. This worked for me: $ alias ...


6

Can you help me understand how to create this alias? May I advise you to create a function if you use bash and put it in .bashrc? mm() { ps -u "$USER" -o pid,rss,command | awk '{print $0}{sum+=$2} END {print "Total", sum/1024, "MB"}' } If it's bash, variables need to be quoted. In a function, no need to put everything on one line.


1

If pick outputs of filename per line, you can set IFS to contain a newline only. Eg (in bash): saved_ifs=$IFS IFS=$'\n' for i in $(pick .??*) ; do ... ; done IFS=$saved_ifs This will allow your filenames to contain spaces and tabs, of course if they contain newlines there will still be a problem. You may also want to consider disabling globbing if there ...


0

find -name "* *" -type f -exec rename 's/ /_/g' {} + Replace the spaces in your filenames with "_". Then use pick. Rationale: Spaces in file names are typically considered a non-standard file naming convention. Successful I.T. is about doing things the same way every time, reminiscent of Sun Tzu's art of war: pick your battles carefully.


-1

I would not use sed instead use egrep -i -v (-i case insensitive -v does not match pattern): egrep -i -v "$(echo ${ARRAY1[@]} | tr ' ' '|')" /etc/file the expression echo ${ARRAY1[@]} because it has no quotes will not print new line making a space delimited string with all the strings in the array. tr ' ' '|' will replace all spaces with \| (or) ...


2

With zsh: array1=( string1 string2 string3 ) sed -i -e/$^array1/d file Would delete all the lines that match any of the regex in $array1. Or you could do (any Bourne-like shell): regexps='string1 string2 string3' grep -ve "$regexps" file Or, to search strings, add the -F: grep -F -v -e$^array1 # (zsh) or: strings='string1 string2 string3' ...


1

Found solution, using "" instead of ''.


0

If for some reason you cannot use single quotes as suggested in Mikel's answer, you can temporarily turn off history expansion using set +H (turn it back on with set -H), as suggested by glenn jackman in comments.


0

I think the problem is that bash on your local machine is interpreting the quotes and not sending them along. Try escaping the quotes with \, ie \". You may have to experiment. Remotely executing commands that require quoting is always tricky. This may not be possible in your environment, but if you can create that long command into a shell script on the ...


4

Just found out how to fix the problem so here is the guide: How to troubleshoot commands that wont execute properly from within a script aka How to view exactly what is being executed by the shell In the script, enclose your problematic command with the set command: set -x #unhide debug info your command set +x #hide debug info In my previous example, ...



Top 50 recent answers are included