Tag Info

Hot answers tagged

31

Yes, we see a number of things like: while read line; do echo $line | cut -c3 done Or worse: for line in `cat file`; do foo=`echo $line | awk '{print $2}'` echo whatever $foo done (don't laugh, I've seen many of those). Generally from shell scripting beginners. Those are naive literal translations of what you would do in imperative languages ...


23

In cd ~/z/ you are using Tilde expansion to expand ~ into your home directory. In BASE="~/z", you are not because you quoted the ~ character, so it is not expanded. That is why you get a message complaining about a nonexistent ~ directory. The solution is to not quote it, i.e. BASE=~/z in order to let the expansion occur.


9

@jw013 has given a valid explanation and solution. But there may be some cases when you do want to quote the paths, e.g. when they contain multiple spaces or special symbols. In this case you should use $HOME instead of ~, i.e. your BASE="~/z" will become BASE="$HOME/z" and work correctly, because parameter substitution is interpreted in double quotes, ...


8

So you want to remove runs of 0s, but only when they are preceded by a colon. sed -e 's/:00*/:/g'


7

w3m is another program that has a --dump option. It is the backend Emacs' most popular web browser.


7

As far as conceptual and legibility goes, shells typically are interested in files. Their "addressable unit" is the file, and the "address" is the file name. Shells have all kinds of methods of testing for file existence, file type, file name formatting (beginning with globbing). Shells have very few primitives for dealing with file contents. Shell ...


6

When your variable is empty, your command becomes: [ -e ] In this case, you call [..] with one argument -e. String "-e" is not null, so test return true. This behavior is defined by POSIX test: In the following list, $1, $2, $3, and $4 represent the arguments presented to test: 0 arguments: Exit false (1). 1 argument: Exit true ...


6

I'm not sure this is better than doing it in memory, but with a sed that reads out its infile for every line in its infile and another on the other side of a pipe alternating Hold space with input lines... cat <<\IN >/tmp/tmp Row1,10 Row2,20 Row3,30 Row4,40 IN </tmp/tmp sed -e 'i\ ' -e 'r /tmp/tmp' | sed -n '/./!n;h;N;/\n$/D;G;s/\n/ /;P;D' ...


5

Replace: if [ -z `cat rvm_check.txt | grep not` ] With: if ! grep -q not rvm_check.txt The reason to use test in an if statement is because it sets an exit code that the shell uses to decide to go to the then or else clause. grep also sets an exit code. Consequently there is no need for test, [, here. grep sets the exit code to success (0), if it ...


5

There are at least two programs named html2text (1) (2) which do this job.


4

elinks has -dump mode too elinks -dump https://www.google.com


4

When you do: nohup echo "45" > my_named_pipe & The shell forks itself. In the child, it opens my_named_pipe for writing. That open() blocks until something else opens my_named_pipe for reading. When you run exit in the parent process, the child is still blocked on the open() and has not executed nohup yet. So upon exiting you shell, the shell ...


4

The ' single quote character in your echo example gets it literal value (and loses its meaning) as it enclosed in double quotes ("). The enclosing characters are the double quotes. What you can do is print the single quotes separately: echo "'"'$a'"'" or escape the $: echo "'\$a'"


4

Well, you could always do it in your shell: while read i; do while read k; do echo "$i $k"; done < sample.txt done < sample.txt It is a good deal slower than your awk solution (on my machine, it took ~11 seconds for 1000 lines, versus ~0.3 seconds in awk) but at least it never holds more than a couple of lines in memory. The loop above ...


4

Here's how to do it in awk so that it doesn't have to store the whole file in an array. This is basically the same algorithm as terdon's. If you like, you can even give it multiple filenames on the command line and it will process each file independently, concatenating the results together. #!/usr/bin/awk -f #Cartesian product of records { file = ...


3

If you're still interested in how to remove any zero except those within the first 4 digits, you can just do like: sed 'H;g;s/\(\n\([^:]*\)\)*0*/\2/g ' <<\IN 2001:0db8:03cd:0000:0000:ef0405:00060:0123 IN OUTPUT 2001:db8:3cd:::ef45:6:123 Ok, so this globally matches either zero or more sequences of a newline followed by zero or more not colon ...


3

A similar approach using awk: awk '{gsub(/:0*/,":")}1' file 2001:db8:3cd:::ef45:6:123


3

You misunderstand the documentation: having it's special meaning inside, would shield $ from the special interpretation "Having its special meaning" means that it is interpreted specially not literally. Single quotes prevent $ from being expanded. But single quotes within double quotes are literal characters i.e. they do not affect anything. If you ...


3

The simplest way I could find is: touch $(paste -d '.' <(printf "%s\n" File{001..005}) \ <(printf "%s\n" {000..004})) This will create File001.000 File002.001 File003.002 File004.003 File005.004 To understand how this works, have a look at what each command prints: $ printf "%s\n" File{001..005} File001 File002 File003 ...


3

It looks to me like that should work, other than the syntactical quibble of missing )s. I tested this and it behaves correctly.. #/bin/bash case "$1" in "-mount") mount /path/to/device /path/to/mountpoint ;; "-unmount") umount /path/to/mountpoint ;; "-remount") "$0" -unmount "$0" -remount ;; *) echo "You have failed ...


2

Get used to quoting variables. Always quote them; that prevents misassessments when it is necessary: if [ -e "$MYAPPPATH" ] This would not have happened with the bash-specific compound command: if [[ -e $MYAPPPATH ]]


2

i dont understand what you are asking but why dont you double click the word/filename that you want to use? when you double click a word it will select the whole word. You can setup breaks so like . or , or < or ( will stop the double click select. These are configureable. I dont have putty so i forget how to configure. Google putty doubleclick word ...


2

ls -t | sed q | xclip for the latest file in the current directory in the buffer


2

If you want to guarantee that they are different, look into mktemp: $ mktemp /tmp/tmp.r8FumWPhn5 $ mktemp XXXXXXXX.tmp Pu4Ii6Sf.tmp


2

That'll be because the PATH is different when running with sudo. Try comparing: which pip env vs. sudo which pip sudo env One secure workaround is to create a symbolic link to pip in /usr/local/bin or even /usr/bin. If you install pip using a package manager it will do this automatically on several (most?) distros.


2

if [ $percentUsed > 75 ] You should use the -gt operator for numeric comparisons; what you are doing is saving the (empty) output of the command "[ $percentUsed ]" into a file named "75". The script is executing properly, however, it is creating a zero byte file by the name 75. Any ideas why this is happening? It is not executing properly. The ...


2

In line 5 you write the contents of $percentUsed to file "75". Instead, try if [ ``echo $percentUsed | sed 's/%//'`` -gt 75 ] which should do what you desire. At least with bash 4.2.25 on Linux it works. Note: Please only use one backtick instead of two shown here - that's because the Stackexchange web platform interprets those backticks.


2

That command will make your computer going to shutdown. $ ssh user@remote_computer sudo poweroff You should notice that those two command will prompt you a password twice. To make it not ask you for a password you should copy you ssh key to your remote computer. And make poweroff command accessible by non root user. To use a list of servers, you could ...


2

I found an easy way to creating files and also directory with random names and extensions: To create random file with random ext use this command: $ mktemp XXX.$(mktemp -u XXX) -u, --dry-run do not create anything; merely print a name (unsafe) And for creating multiple files use above command inside a loop: $ for create in $(seq 5); do ...


2

The problem you're having, and the reason it's shell-dependent, is that the > redirection is processed first, and if nobody is reading the pipe yet, it will block. Only after the open succeeds will the shell execute nohup. You disconnected while it was still blocked in open, so nohup hadn't happened yet and the SIGHUP killed the background process. This ...



Only top voted, non community-wiki answers of a minimum length are eligible