Tag Info

Hot answers tagged

17

I don't encounter that behavior. But I was able to reproduce it. Maybe you accidentially changed the option cdspell. See the Bash Reference Manual: cdspell If set, minor errors in the spelling of a directory component in a cd command will be corrected. The errors checked for are transposed characters, a missing character, and a character too many. ...


17

I don't have an OSX system handy to check on but on all *nixes, ~foo is a shorthand for the home directory of user foo. For example, this command will move into my user's $HOME (cd ~ alone will move into your home directory): cd ~terdon So, ~ and Tab will expand to all possible user names. The list should be the same as the list of users in /etc/passwd. ...


10

$file | grep -o executes the command specified by the value of file and pipes its output to grep. But that's clearly not what you wanted. If you want to list files that contain o You meant the value of file to be an input file for grep, not a command to execute. So you need an input redirection, not a pipe. if grep 'o' <"$file" grep reads from ...


6

fail_color="\033[31;1m" color_end="\033[0m" function="foo" line_number="42" printf "%bError - Function: %s, Line: %d%b\n" "$fail_color" "$function" "$line_number" "$color_end" Output: Error - Function: foo, Line: 42 Tested with Ubuntu 11.04 (bash 4.2.8(1)-release), Ubuntu 14.04.1 LTS (bash 4.3.11(1)-release), RHEL 5.1 (bash 3.1.17(1)-release), RHEL ...


5

You don't need to loop, you can tell cat to read all the files: cat /var/abc/*.csv > file1.csv && rm /var/abc/*.csv as long as there aren't too many files (but the limit is huge). Using && between the two commands ensures the files are only deleted if they were successfully "copied". There are a couple of caveats though: you mustn't ...


5

Easiest way? Use an editor with syntax coloring that's aware of the shell you're using, and inspect visually. When you get a big swathe of string color, you know you left out a quote. Just about any decent programming editor has syntax coloring. Syntax coloring sometimes gets it wrong, but that's usually a sign that your program is too complex and humans ...


5

You're trying to execute $file. Instead, you must echo it: # ... if echo "$file" | grep 'o' ; # ... Note that the grep will already print the filename, so you should silence it (e.g. grep -q 'o' or grep 'o' >/dev/null). You're also passing -l to ls, which you don't want to do. ls -l prints the file name and attributes, and you'll be matching against ...


5

The root directory is /. The themes directory in the root directory is /themes. A path that starts with / is called an absolute path; it starts from the root directory. A path that doesn't start with / is called a relative path; it starts from the current directory of the program where you use it. For example, a bare file name with no directory indication is ...


4

An external linting tool like ShellCheck can detect problems and may have better messages and locations than bash itself. For a program that is mostly echo statements with a "${MY_ARRAY[1]" in the middle, ShellCheck tells you that it couldn't parse the quoted string. It even pins the problem down to the $ character and hints that the parameter expansion was ...


4

If you have newer files on the old disk that you want to ignore I would go about it like this Create a temporary marker file with a modified-by date that separates files I want from those I don't Copy files older than the marker file to the new location Here are sample commands for this, which assume you want to maintain any directory hierarchy from the ...


4

Do not use ls. It's not recommended to use in such cases. Moreover using grep to filter according to date is not a good idea. You filename might itself contain 2012 string, even though it was not modified in 2012. Use find command and pipe its output. find . -newermt 20120101 -not -newermt 20130101 -print0 | xargs -0 cp -t /your/target/directory Here, ...


4

If you reverse the file, you can add a line the first time you see something: tac lists.txt | awk -v l1="list1" -v val1="something new" \ -v l2="list2" -v val2="another thing" ' index($0, l1"[i++]") && !found1 { printf "%s[i++] = \"%s\";\n", l1, val1 found1 = 1 } index($0, l2"[i++]") ...


3

Depending on what your ultimate aim is, you could use printf: $ a=(1 2 3) $ printf "foo %s bar\n" "${a[@]}" foo 1 bar foo 2 bar foo 3 bar printf re-uses the format string until all the arguments are used up, so it provides an easy way to apply some formatting to a set of strings.


3

If timeout times out, it exits with status 124; you can check this to determine whether the script timed out or not.


3

That code works for me with all versions of bash I tried between 2.05b and 4.3. More likely you tried to run that script with a different shell that doesn't support the $'...' form of quoting. That $'...' syntax is not standard sh syntax (yet) and only supported (as of 2015-05-22 and AFAIK) by ksh93 (where it originated), zsh, bash, recent versions of mksh ...


3

I like Cyrus's answer, but this syntax also works: #!/usr/bin/env bash fail_color=$'\033[31;1m' color_end=$'\033[0m' function="foo" line_number="42" printf "%sError - Function: %s, Line: %d%s\n" "$fail_color" "$function" "$line_number" "$color_end" And ShellCheck says "It all looks good!". :)


3

The first problem is that you have a space after the =: TEMP= grep -o [1-9][0-9]\.[0-9] /tmp/temp.txt That's wrong and breaks your script. The next issue is that you're assigning the commands themselves, as strings, to the variables and not their output: $ foo=date $ echo $foo date To run the command and save its output to a variable, you need to use ...


3

Since you're trying with sed ranges, here's one possible way to do it. The lines in your additional-values.txt follow the same pattern: KEY[i++] = 'VALUE'; //etc and as far as I can tell, each line should be inserted in a range that is always delimited by var KEY = new Array(); and an empty line so you could process additional-values.txt and turn it ...


3

In your script file named hexto64, simply write : #!/bin/bash printf "%s" "$1" | xxd -r -p | base64 And then you can use it as such : hexto64 49276d2 Just so you know, $1 means the first parameter you gave after the program name : 49276d2 in our case.


3

Yeah, actually there is: set . ~/Development{,/resources} ~ IFS=:; CDPATH="$*" The "$*" special parameter substitutes the first character in $IFS between each of the positional parameters. So if you set them and expand the arguments you want as separate arguments, you can then quickly fill in the appropriate delimiters as necessary on assignment. If, as ...


2

How about this? find "`php-config --extension-dir`" -name "*.so" | sed 's!^.*/!extension=!' For each line, sed will match the longest string that starts at the beginning (^) and ends at /. It will always match everything up to and including the last /, because it is a greedy match, i.e., it will match everything but the filename. It then replaces that ...


2

You don't need test command when using case, and don't need case when using test: case $1 in ("") echo "something" ;; esac and: [[ -z $1 ]] && echo "something" or using old test [...] for portability: [ -z "$1" ] && echo "something"


2

A low-tech approach is tr -cd "'\n" < run_me.sh | awk 'length%2==1 {print NR, $0}' The tr deletes all characters except for single quotes and newlines, and the awk identifies the lines that have an odd numbers of characters (i.e., non-matching quotes).  Check them out individually; note that valid strings like "That's not a bug!" will be flagged.  If ...


2

p='* "foo ' s=' bar $USER' CATEGORIES=(one two three four) CATEGORIES=("${CATEGORIES[@]/#/$p}") CATEGORIES=("${CATEGORIES[@]/%/$s}") paste <(printf '[%s]\n' "${!CATEGORIES[@]}") \ <(printf '%s\n' "${CATEGORIES[@]}") Output: [0] * "foo one bar $USER [1] * "foo two bar $USER [2] * "foo three bar $USER [3] * "foo four bar $USER ...


2

Assuming vcgencmd measure_temp will create a single fixed point number, this command may do what you want: temp=$( vcgencmd measure_temp | grep -o '[0-9]*\.[0-9]' ) date "+%Y-%m-%d %H:%M;${temp}" >> /home/pi/temp_hist.csv


2

If the lists in your input file is separated by a blank line, you can use a tool that lets you set the record separator (what defines a "line") to consecutive newlines. For example, in Perl (assuming your replacements are in a file called additions): perl -ne 'BEGIN{## Open the additions file open($fh,"additions"); ...


2

gzip is encoding the filename of the input file into its output. Even with -c option it does this. You can see this with gzip -c some_file | strings|head -1. however, when reading from stdin, gzip does not do that, since it doesn't know the filename. You can tell gzip to omit from output the filename and time-stamp with -n.


2

Remove the $ from the assignment line inside the loop. You're trying to use the value as the name of the variable you're assigning to, which is causing Very Strange Behavior.


2

This is how I would do this: find /var/backup/ -type f -maxdetph 1 $(printf "! -name %s " ${keep[*]}) \ -exec rm {} \; Note that this will not work if your file names have spaces in them.


2

Try using awk: awk -F'"' '{ print $2 }' conf.txt



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