New answers tagged

1

It should work by default. E.g. if I do ls --color=always > /tmp/a and than cat /tmp/a, I see the colors. Checking with od confirms that the file uses ANSI colors. So I think you should check if your terminal supports ANSI colors (and they are enabled).


0

The best solution is the manual for bash, yes the simple man bash. To find the exact entry in the manual, execute this command: LESS=+/'^CONDITIONAL EXPRESSIONS' man bash When working inside a bash shell, this also helps: help test


1

Believe it or not, [ is actually another name for test command. You can check the man page: man test test is a shell builtin too in most shells (and it gets precedence over the external one): % type -a \[ [ is a shell builtin [ is /usr/bin/[ % type -a test test is a shell builtin test is /usr/bin/test In bash you can also see help test.


0

(in bash) The exact answer to your request is to use GLOBIGNORE: By executing this command: LESS=+/'^ *GLOBIGNORE' man bash read: GLOBIGNORE A colon-separated list of patterns defining the set of filenames to be ignored by pathname expansion. If a filename matched by a pathname expansion pattern also matches one of the patterns in GLOBIGNORE, it ...


1

To answer your question, the problem is with your usage of grep. Grep takes what you put as a literal, so if you grep with *.log, it will actively look for the character star * followed by .log, so it won't find anything. As a quick test, The command echo hello.log | grep "*.log" will return nothing The command echo hello.log | grep ".log" will return ...


1

Use -1 option: ls -1 * I just found that out today, too: http://askubuntu.com/questions/733152/ls-output-not-compact-anymore I wanted to do the exact opposite...


-1

I did it ! i erased all in sudo crontab -e and entered in crontab -e only this line * * * * * sudo bash -x /home/pi/deleteoldfiles.sh > /home/pi/output.txt 2>&1 and it's working, i have a log file which i can track if it`s working good. Thank you all !


1

You could make small edit to your original idea with arrays - use arrayName[number]="tab assignment" form . Here's for instance what I did in interactive terminal session to open new terminal window ( and all these steps could easily be turned into a script ). $ array[0]=" --tab --title 'Tab1' -e vi" $ array[1]=" --tab --title ...


5

You can use \ to split long commands over multiple lines. Example: #!/bin/bash echo "Hello World!" echo \ "Hello World!" running this script results in $ ./test.sh Hello World! Hello World! In your case you can use something like #!/bin/bash gnome-terminal \ --tab --title="Zookeeper" --profile Hold -e "sh -c ...


4

Try it with writing \. Then the shell ignores the next new line and you can write options in a new line.


1

< is a redirection operator. It takes the contents of the file/output from the right and pass it to the command to the left.


0

With GNU Parallel: parallel --tag 'grep -oh Author {} | wc -w' ::: *.dat If there are subdirs: find . -name '*.dat' | parallel --tag 'grep -oh Author {} | wc -w'


0

#!/usr/bin/env python import os from os.path import expanduser, isdir, join, pathsep def list_executables(): paths = os.environ["PATH"].split(pathsep) executables = [] for path in filter(isdir, paths): for file_ in os.listdir(path): if os.access(join(path, file_), os.X_OK): executables.append(file_) return ...


2

You are almost there except you need the r modifier do a reverse sort: grep -rc "Pattern" . | sort -t: -k2,2nr check the r modifier after n.


1

You can use this, ~]# cat test data_x:12 data_y:34 data_z:56 data_a:205 data_b:1003 ~]# sort -k2 -n -r -t: test data_b:1003 data_a:205 data_z:56 data_y:34 data_x:12 So for your code, you can do ~]# grep -rc "Pattern" . | sort -k2 -n -r -t:


0

Doing SRA[111-190] should suffice to get the directory names. Another method would be to use brace expansion: SRA{111..190} {111..190} will be expanded to 111,112,113,114....190. So you will eventually get: SRA111,SRA112,SRA113,SRA114.......SRA190 Now to get all files inside these directories, use pattern: SRA{111..190}/*


0

This script may help. (please remove the echo to actually mv files): #!/bin/bash shopt -s nullglob month=(Jan Feb Mar May Apr Jun Jul Aug Sep Oct Nov Dec) for y in 2016; do for m in {01..12}; do fn="$y-$m" dn="${month[10#$m-1]}_$y" [[ ! -d $dn ]] && mkdir -p "$dn" for file in ./"$fn"*.wav; do echo ...


0

### capitalization is important. Space separated. ### Null is a month 0 space filler and has to be there for ease of use later. MONTHS=(Null Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) cd /your/ftp/dir ### pretty obvious I think for file in *.wav ### we are going to loop for .wav files do ...


1

Disregarding that your awk is missing a brace, and assuming list contains a single line, why not: NAME=$( cat list | awk '{print $1".modifications_to_name"') && checkStatus | grep pertinentinfo | cleanupFormatOfPertinentInfo | sendAlert $NAME If you want to iterate through a list with multiple lines and have name evaluate to something different each ...


0

You could use the exit code in an if statement: #!/bin/bash cat config if [[ "$?" -ne "0" ]]; then echo "VALUE" fi If the previous command succeeds it will exit with a "0" value. So you are telling it that on any other exit (failures) it will output whatever is in the "VALUE".


1

Your construct is fine. You could even do someting like cat config || cat defaultconfig If you use some random command (like the ./get_config_from_web in comments), you'll have to make sure the command does give a sensible return status. That can be tricky, shell scripts just return the result of the last command executed, you'd have to do a exit if you ...


1

The following will echo 42 for any noncaught error condition: trap "echo 42" ERR You can make this a configurable variable: trap 'echo "${CONFIG:=42}"' ERR # if $CONFIG is not set, it will be defaulted to 42


0

if [ -a config ]; then cat config; else echo ${DEFAULT};fi assuming the variable DEFAULT is set to something beforehand.


1

A while loop with suitable input can do the job, assuming no IFS-thwarting characters in the directory listings. paste <(ls /var) <(ls /usr) | while read e u; do echo $e $u; done


4

You specify the path to a desired destination from your working directory, so in your case you could just put: > output/output.txt Paths are always taken from your current/working directory, unless they start with '/' or '~'. In these cases, the rest of the path is relative to the root or your home directory respectively. (There may be other examples ...


4

the reference to a subdirectory of the current directory would be ./subdir/filename or simply subdir/filename. in your example, if you are in ~/workspace/pset3/find and address the output.txt file, you can reference it as ./output/output.txt or output/output.txt


0

Could you tell the error, that you get after execution, because the script is working for me. There is the same question here, answered using php and perl: Output URL of open firefox tabs in terminal Since the main part in python, here is a purely python script, to do the same thing, try it as well: #!/usr/bin/python import json f = ...


0

A sub-shell environment does not need to live in a separate process, it just needs to duplicate the current execution environment. In ksh93 this is done by a virtual sub-shell mechanism that does not call fork(). This makes ksh93 very fast on archaic platforms like Win-DOS, as Win-DOS is extremely slow with forking. sh -c cmd on the other side creates a new ...


2

You have mistaken, double quotes "$b" does not prevent parameter expansion, it prevents pathname expansion (aka globbing) and fields splitting. If you want to prevent parameter expansion, you need to use quoting, like single quote '$b' or escaping \$b: $ echo '$b' or: $ echo \$b then $b is output literal. In the example, there's nothing prevent ...


0

An additional bash solution: It will handle unmatched pairs of braces {. Won't accept a closing brace until one or more opening braces appear. Will reset the count of braces to 0 at the end of the line. Will accept a comma as valid after more closing braces than opening braces. Will remove one space in front of solution. Will quote the resulting word. ...


0

Quote your variables and use IFS= with read, as this: displayLine(){ printf '%s\n' "$line"; } filename="infile" while IFS= read -r line do displayLine "$line" done < "$filename"


-1

num is a tiny awk wrapper which exactly does this and more $ echo "1 2 3 4 5 6 7 8 9" | num max 9 $ echo "1 2 3 4 5 6 7 8 9" | num min max median mean ..and so on It's battletested and saves you from reinventing the wheel in the ultra-portable awk.


3

Put this in your ~/.inputrc: set show-all-if-ambiguous on For additional credit, add: set completion-ignore-case on All of the options are in the GNU manual...


1

Tools like tr and wc can operate on files; you don't have to pass strings to their stdin. To answer your question: how would I go about having the text.txt as a variable so that I can just call the variable in the script? The syntax for that is: input=text.txt # or other file name tr '[A-Z]' '[a-z]' $input


0

You can use simple command substitution: variable=`cat text.txt` echo $variable Or in bash: variable=$(cat text.txt) Same with: variable=`tr '[A-Z]' '[a-z]' < text.txt`


1

A subshell that duplicates the existing shell. It has the same variables¹, the same functions, the same options, etc. Under the hood, a subshell is created with the fork system call²; the child process goes on to do what is expected of it while the parent waits (e.g. $(…)) or goes on with its life (e.g. … &) or otherwise does what is expected of it (… | ...


0

Where you send email from your script, right after sending the email, put this command: date +%s > /var/tmp/lastemail.out then convert your emailing part to something like this lastemail=$(cat /var/tmp/lastemail.out) now=$(date +%s) (( timediff=${now}-${lastemail} )) if [ ${timediff} -ge 3600 ] then sendmail me@mydomain.com < mydiskreport.txt ...


1

On UNIX systems wildcards are handled by the shell. The escaped * in the following command is passed to cp which looks a file named /media/* and presumably that doesn't exist. cp -p -r /media/\* $pth In Windows, by contrast, the * is not handled by the command processor but by the copy command. Only root is allowed to change file ownership. Otherwise, ...


0

I was able to fix this on my Mac OS X El Capitan by installing gnu coreutils Install Homebrew Then install coretuils brew install coreutils Then set PATH in your ~/.zshrc export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"


0

for dir in $(find DIR1 -type d) ;do newdir=$(echo ${dir} | sed -e "1,1s/^DIR1/DIR3/") mkdir -p ${newdir} done This block above creates all directories in DIR1 under DIR3. If you are creating new directories under DIR1, which are not in DIR2, you should run this snippet when there are new directories under DIR1, otherwise, running this once will be ...


1

POSIX-compliant sed solution which produces 0019093203 from the sample data (assumes Invoice information follows "ODSTATION BUSY"): sed '/.*ODSTATION BUSY.*Invoice #: */!d; s///; s/\\n.*//' file A tweaked version of don_crissti's approach to handle either case: sed -n '/ODSTATION BUSY/s/.*Invoice #: \([0-9]\{1,\}\)\\n.*/\1/p' file


1

perl -e 'while(<>) {if ($_ =~ qr/ODSTATION BUSY/) { print "$1\n" if $_ =~ /Invoice #:\s+(\d+)/ } }' <yourfile_goes_here> Oh, Bash? grep 'ODSTATION BUSY' filename |egrep -o 'Invoice[^0-9]+[0-9]+'|egrep -o '[[:digit:]]+' Or: awk 'BEGIN{$0 ~ /ODSTATION BUSY/}; gsub(/^.*Invoice #: /,""){print $1}' filename |sed 's/\\n.*$//g'


0

if there are too many lines, like in the order of thousands, I prefer to use a temporary file rather than extracting them in the memory and causing system load, but this is a preference of mine. grep "ODSTATION BUSY" logfile > workfile cat workfile | while read line do invoffset=$(echo ${line} | grep -b -o "Invoice #:"|cut -d: -f1) #locates string ...


0

One way of doing this would be: $ grep "ODSTATION BUSY" <filename> | sed 's/^.*Invoice #://;s/\n.*$/' That's pretty ugly, I'm sure there are other, possibly better, ways of pulling this information out using something like awk.


0

You can output the default if the grep fails: var=$( grep -e '^setting_1' || echo default_setting ) Note that you didn't close the quotes (and without the file argument, grep will filter stdin).


2

I think one way to print information about function, when you test the required arguments and exit if they don't exist - and display some messages is to change bash builtin return and/or exit in the beginning of every script (or in some file, that you source every time before executing program). So you type #!/bin/bash return () { if [ ...


2

As noted in comments, creating certificates (signed host keys) would help you to solve this issue (see manual page for ssh-keygen, chapter "Certificates" describing this procedure). Basically you would create a CA on server, sign the host keys and store the CA public key in your known_hosts such as: @cert-authority * ssh-rsa AAAAB5W... The other ...


2

With the GNU implementation of grep, you have the -h/--no-filename option for that. $ grep -rc PATTERN . ./b:1 ./a:0 ./1/2/c:2 $ grep -rhc PATTERN . 1 0 2 The portable/standard equivalent would be: $ find . -type f -exec grep -c PATTERN {} \; 0 2 1 but would mean running one grep invocation per file. If instead, you want the total number of ...


0

your variables get evaluated only the first time , you must put them inside the ps1 to be treated literally ffirst time and get evaluated after command execution . PS1='|me::\W| $( mytest=$PWD;mynext="${PWD##*/}"; mylength=$((${#mytest}+${#mynext})) ; length=$(($mylength+6)) ; printf "\\u2500%.0s" $(seq $length $(tput cols))) \w\n\$' you can ...


2

Command substitution causes a normal call to the shell interpreter and this interpreter will run the echo command in a sub-process. The sub-process is needed as the shell needs to establish a pipe to the echo command in order to be able to read the results. The sub-process that runs the echo command is created by fork() and this creates copies of all ...



Top 50 recent answers are included