Hot answers tagged

6

Here's a more "bashy" and concise approach: #!/bin/bash ## Read the UUIDs into the array 'uuids'. Using awk ## lets us both skip comments and only keep the UUID mapfile -t uuids < <(awk '!/^\s*#/{print $1}' uuids.txt) ## Iterate over each UUID for uuid in ${uuids[@]}; do ## Set the special array $_ (the positional parameters: $1, $2 etc) ...


5

Walk over the files, create an associative array over the uuids contained in their names (I used parameter expansion to extract the uuid). The, read the list, check the associative array for each uuid and report whether the file was recorded or not. #!/bin/bash uuid_list=... declare -A file_for for file in *_*_* ; do uuid=${file%%_*} ...


5

This is pure Bash (i.e. no external commands), and it's the most coincise approach that I can think of. But performance-wise is really not much better than what you currently have. It will read each line from path/to/file; for each line, it will store the first field in $uuid and prints a message if a file matching the pattern path/to/directory/$uuid* is ...


5

This snippet: # Add git branch if its present to PS1 parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' } if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ ' else ...


5

When you run sh rBackup.sh the script rBackup.sh is being run by sh (dash) which does not support brace expansion. On the other hand, when you do ./rBackup.sh then the #!/bin/bash first line specifies the exact interpreter to use. It happens that bash supports brace expansion. If you don't include a valid executable in the shebang line, you are ...


4

Keep in mind that sh actually calls dash, which is limited compared to, say, Bash. As was observed by @Serg in a comment on the question, the curly braces are being interpreted as inputs to dash, possibly, which is why it won't work. This is why it's failing - Dash doesn't do Brace Expansion. You're probably expecting the system to use Bash style ...


4

cp does not support any wildcards by itself. Expansion of wildcards and patterns is done by the shell, which is bash by default. In any case, character classes are to be specified within [] - that excludes the [] already surrounding them. Like so: cp Downloads/[[:alnum:]] test/ However, [[:alnum:]] will only match a single alphanumeric character. To ...


3

Using Perl: perl -F'\|\|\|' -lane '$, = "\t"; @f = split(/;|\|/, $F[1]); shift(@f); splice(@f, 5); print(@f)' file -F'\|\|\|': sets the input field separator to |||; -l: enables automatic line-ending processing. It has two separate effects. First, it automatically chomps $/ (the input record separator) when used with -n or -p. Second, it assigns $\ (the ...


2

One of the more basic relationships between a program and the parent shell is the concept of an "exit code". This is a simple integer that —by convention— lets the shell know there was a problem. Zero is "everything went fine", anything else is "omghoustonproblems". Again this is just a convention. Some things (eg grep -q ...) use the exit code to indicate ...


2

[:alnum:] actually matches a single character from the set :, a, l, n, u, m i.e. given $ ls _?_ 123 7 a abc :file fi:le m then $ ls [:alnum:] a m To match a single character from the alphanumeric class [:alnum:] it needs to be [[:alnum:]] $ ls [[:alnum:]] 7 a m To match a single alphanumeric character followed by zero or more arbitrary ...


2

Let's use sed: sed -r 's/.*\|\|\|;(CSQ[^|]+)\|([^|]+)\|([^|]+)\|([^|]+)\|([^|]+)\|.*/\1\t\2\t\3\t\4\t\5/' file.txt python is not fast at manipulating very large file, this would be much faster than python. Example: % cat file.txt 2 41620 . T G 100 PASS ...


2

This should work for you: cut -d"|" -f4,5,6,7,8 filename.txt | sed 's/;//g' | sed 's/|/\t/g' Example: $ echo "2 41620 . T G 100 PASS ...


2

tacmd listsystems | grep -i $i | awk '{print $1}' >> offline.lst The >> operator tells bash to append output to a file.


2

unset IFS set -f set +f -- $(<uuid_file) while [ "${1+:}" ] do : < "$source_directory/$1"* && printf 'File for %s has arrived.\n' "$2" shift 2 done The idea here is not to worry about reporting errors the shell will report for you. If you try to < open a file which doesn't exist your shell will complain. In fact, it ...


1

There's no way how to use the exclamation mark in double quotes. Backslash it unquoted, or use single quotes instead of double quotes, as special characters aren't expanded in single quotes. curl -ik "http://localhost/index.php?username=parto&password=hello"\!"23" curl -ik 'http://localhost/index.php?username=parto&password=hello!23'


1

The file was getting sourced, but execution was stopped when it reached this case statement, which will only continue if it is being sourced in a shell in interactive mode. case $- in *i*) ;; *) return;; esac The variable $- represents the flags with which the current shell was initialized. If it doesn't see i (the flag for interactive mode), ...


1

The way I'd approach it is to get uuids from file first, then use find awk '{print $1}' listfile.txt | while read fileName;do find /etc -name "$fileName*" -printf "%p FOUND\n" 2> /dev/null;done For readabilty, awk '{print $1}' listfile.txt | \ while read fileName;do \ find /etc -name "$fileName*" -printf "%p FOUND\n" 2> /dev/null; ...


1

Nothing's wrong with tee. Python buffers output if it detects it's not writing to a TTY. See this SO post. Use sys.stdout.flush() to force flushing the buffer.



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