Tag Info

New answers tagged

5

What jimmij said. His last example is the closest you can get to what you're attempting in your piped expression. Here's a variant on that theme: echo 'hello world'|echo $(read s;s=${s^^};echo ${s// /_}) I'd be inclined to use tr, as it's quite fast. echo 'hello world'|tr ' [:lower:]' '_[:upper:]' I suppose it's a shame that bash doesn't permit ...


4

You cannot pass parameter expansions in such a way. When you refer to x using $ symbol as in "${x}" form, then it has to be real variable name, not a standard input, at least not in bash. In zsh you can perform nested parameter substitutions in the following way: $ x=''hello world' $ echo ${${x// /_}:u} HELLO_WORLD (note: :u is for zsh the same as ^^ for ...


0

First I'd look in /etc/bash_completion It should look like . /usr/share/bash-completion/bash_completion (don't miss the . in the start of line) If it did not help I'll try to find all links in bash files find / -name *bash* -type f -exec grep -l completion {} \+ 2>/dev/null and check it for mistakes. In some file should be following code if ! shopt ...


0

xml-cut from xml-coreutils may accomplish your needs


3

That software hooks the write() system calls that apprear to write at file descriptor 2, that is known as stderr. It is a shared library (so recompilation of the kernel is not necessary). As descripted in the installation manual it takes use of the environment varaible LD_PRELOAD. The dynamic linker can be influenced into modifying its behavior during the ...


3

It appears that the program is re-writting the various write() functions to detect whether you are printing to file descriptor 2 and then adding the relevant escape codes to make the output red at the terminal. Unfortunately, in shell, when you do something like echo "foo" 1>&2 The function will still be calling write (or some other similar ...


-1

According to instruction from https://github.com/sickill/stderred you should add to your script export LD_PRELOAD="/path/to/stderred/\$LIB/libstderred.so${LD_PRELOAD:+:$LD_PRELOAD}" where the $LIB may should be substituted by hand onto you architecture


0

I think the follow should help. This is usual task which should to do between two limits. unset f t while read l do t=${t:+$t\\n}$l l=${l//[<>]/} : ${f:="$l"} if [ "$l" = "/$f" ] then echo -e "$t" > "$f".xml unset f t fi done < <(cat your_xml_file)


0

Regarding other solutions from Costas or Kannan: This if ! mv $file $HOME/linux/scripts should be replaced with if ! mv "$file" "$HOME/linux/scripts/." Also add this line somewhere at script start mkdir -p "$HOME/linux/scripts" Otherwise when linux/scripts is missing, every file will be moved there as a file and overwriting the previous. And if ...


2

I don't think this is possible in dash. As far as I can tell from its man page, it has no support for process substitution. As a workaround, you could try what mikserv suggested, or you can redirect everything to a file, and then after your script is finished (presumably this is in a script), add that file's contents to logger: $ exec > ~/foo/foo.txt $ ...


3

You can just do: { commands .... } | logger -t my_awesome_script You can do that with any shell. If you don't like the way it looks, maybe make the script wrap itself in a function. #!/bin/sh run() if [ "$run" != "$$" ] then run=$$ exec "$0" "$@" | logger -t my-awesome-script fi #script-body run || do stuff


4

First, a summary of some facts about signals and the shell: When you press CTRL+C at the keyboard, a SIGINT is sent to all processes in the process group of the foreground process. In this case, it means a SIGINT is will be received by both the cat command and the bash process interpreting your script. When you trap INT, an INT will no longer cause the ...


1

In most cases command test ( [ ) offer operators -a and -o EXPR1 -a EXPR2 True if both expr1 AND expr2 are true. EXPR1 -o EXPR2 True if either expr1 OR expr2 is true. But in the case of possible multi-lines you should use command which can operate in the condition (for example ls) ls {town,city}-*.swf &>/dev/null && mkdir town You ...


2

I think it is wrong way to kill error message. As for me much better to use it for check. #!/bin/bash if ls *.sh &> /dev/null then echo "Moving all script files to script directory..." mv -v -n *.sh $HOME/linux/scripts else echo "No files with .sh suffix -- nothing to move" fi


1

The below script should be helpful, it will help you in displaying your custom error messages. #!/bin/bash files=$(ls *.sh 2> /dev/null) if [ -z "$files" ] then echo "No script files found. Exiting.." exit fi echo "Moving all script files to script directory..." for file in $files do if ! mv $file $HOME/linux/scripts then echo ...


1

Add 2>/dev/null to you mv command to redirect stderr to /dev/null: mv *.sh $HOME/linux/scripts 2>/dev/null


0

Make some fake test files: (only for this example) $ touch filename_{1000000000..1000000099..5}.gz Grab a "time" range of files from ls output and pass this to echo: $ echo $(ls | awk -F'[_,]' '1000000044<=$2 && $2<=1000000066') filename_1000000045.gz filename_1000000050.gz filename_1000000055.gz filename_1000000060.gz ...


0

Not as simple, but my 'weapon of choice' python my_mrjob.py $( for f in {1413324000..1413410400}; do [ -f filename_$f.gz ] && echo $f; done ) PS: IMHO, the python job itself should be modified to allow for range entry, with additional intelligence to skip over non existent files. Will be much faster and simpler. The {a..b} syntax is not a lazy ...


1

Your first two commands are not doing anything. You are catting the two files and passing them to grep which is ignoring it since you have given it a file to search in. You only needed grep -Ff text.txt vendor.xml | sort -u | uniq -c That doesn't work the way you expected because grep is smarter than that. When you give it a list of patterns to look for, ...


3

Here's an awk answer: awk 'NR==FNR {count[$0]++; next} $1 in count {print count[$1],$0}' text.txt vendor.xml | sort -nr


6

join combines the files (needing sorted inputs): $ join <(sort text.txt) <(sort vendor.xml) 00:10:f6 vendor="micro" 00:10:f6 vendor="micro" 03:48:03 vendor="apple" 8f:91:34 vendor="dell" 93:ab:c6 vendor="sun" So all what's left is to add uniq -c to do the counting: $ join <(sort text.txt) <(sort vendor.xml) | uniq -c 2 00:10:f6 ...


0

How about gksudo? $ gksudo your_app_launcher.sh It does show a graphical dialog for safely entering the administrator password.


1

POSIX defines the -p option to the pathchk utility so... -p Instead of performing checks based on the underlying file system, write a diagnostic for each pathname operand that: Is longer than {_POSIX_PATH_MAX} bytes (see Minimum Values in the Base Definitions volume of IEEE Std 1003.1-2001, Chapter 13, Headers, ) Contains any component longer than ...


6

Process substitution results in a special file (like /dev/fd/63 in your example) that behaves like the read end of a named pipe. This file can be opened and read, but not written, not seeked. Commands that treat their arguments as pure streams work while commands that expect to seek in files they are given (or write to them) won't work. The kind of command ...


0

Here's a solution that's lower impact than running the external command ls, as well as being more portable than a solution based on stat (which differs from OS to OS). #!/bin/bash found=false for _ in town-*.swf, city-*.swf do found=true; break done if $found; then echo "Yup!" fi The underscore is a throwaway variable. The for loop is an easy way ...


0

If you only want to backup the directory you specify this should work: Place this script in the directory you want to backup: #!/bin/bash #Check if "backups" exists / if not create folder if [ ! -d "backups" ] then mkdir backups fi #Begin Backup #Choose if you want to use gzip or just tar #TAR with gzip #tar -cvfz --exclude=backups ...


9

Yes, you are looking for the exit code but that is completely irrelevant here. The test you ran will print VULNERABLE to your terminal if the version of bash you are using is vulnerable. If you don't see it, you passed the test. While programs can indeed return information that is not necessarily shown to the user, that is not relevant to the issue here. ...


1

A simple 1 liner solution: find -name "*.flac" -exec ffmpeg -i {} -acodec libmp3lame -ab 128k {}.mp3 \; http://lewisdiamond.blogspot.ca/2012/01/converting-flac-to-mp3.html Note that this will be applied recursively in the given directory. I.e. if you run this from your Music folder, it will convert all flacs from subfolders and produce a .mp3 next to it. ...


4

POSIXly, you can use ls if ls town-*.swf >/dev/null 2>&1 && ls city-*.swf >/dev/null 2>&1 then mkdir towns fi or shorter if condition: if ls town-*.swf city-*.swf >/dev/null 2>&1 even if your shell supports brace expansion: if ls {town,city}-*.swf >/dev/null 2>&1


4

if stat -t city-*.swf >/dev/null 2>&1 then if stat -t town-*.swf >/dev/null 2>&1 then mkdir towns fi fi As user uwe pointed out in the comments, my previous command would prevent the wild card from being expanded. However, this new version should work. If you need it with a single if loop, you could modify the ...


11

shopt -s extglob echo rm foo.!(org) This is "foo." followed by anything NOT "org" ref: https://www.gnu.org/software/bash/manual/bashref.html#Pattern-Matching


0

One more way of doing it is using redirections within functions. #!/bin/bash function1 () { echo 'STDOUT from function 1' echo 'STDERR from function 1' >&2 } function2 () { echo 'STDOUT from function 2' echo 'STDERR from function 2' >&2 } function3 () { echo 'STDOUT from function 3' echo 'STDERR from function 3' ...


4

alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'" ls -R: list subdirectories recursively grep ":$": grep only for lines with : at the end of the line sed -e 's/:$//': remove : at the end of the line -e 's/[^-][^\/]*\//--/g': replace all path components except of last dir with --. To be precise replace any ...


0

Though I had to remove the last comma in your example input to make it work because jq was complaining about expecting another array element, this: INPUT | jq -r '[.[][].displayName], [.[][].value]| join(", ")' ...got me... First Name, Last Name, Position, Company Name, Country VALUE, VALUE, VALUE, VALUE, VALUE How it works in a nutshell: I traversed ...


0

Since you tagged this python and assuming name of json file is x.json import os, json with open('x.json') as f: x = json.load(f) print '{}{}{}'.format(', '.join(y['displayName'] for y in x['data']), os.linesep, ', '.join(y['value'] for y in x['data'])) First Name, Last Name, Position, Company Name, Country VALUE, VALUE, VALUE, VALUE, ...


1

I've found jq hard to wrap my head around. Here's some Ruby: ruby -rjson -rcsv -e ' data = JSON.parse(File.read "file.json") data["data"].collect {|item| [item["displayName"], item["value"]]} .transpose .each {|row| puts row.to_csv} ' First Name,Last Name,Position,Company Name,Country VALUE,VALUE,VALUE,VALUE,VALUE The ...


3

Given just this file, you can do something like: <testfile jq -r '.data | map(.displayName), map(.value) | join(", ")' The . operator selects a field from an object/hash. Thus, we start with .data, which returns the array with the data in it. We then map over the array twice, first selecting the displayName, then selecting the value, giving us two ...


1

Use the range operator: echo {1413324000..1413324199}.gz Similarly, $python my_mrjob.py {1413324000..1413324199}.gz If files are not there for every milli second: $python my_mrjob.py $(ls {1413324000..1413324199}.gz 2>/dev/null)


0

This does only the two-color highlight for path and filename, not the per-filetype thing of ls: Configure the colors of grep output in the right way for matched and unmatched part, and match the filename: $ export GREP_COLORS="sl=0;33;49:ms=1;34;49" $ find /etc/ -type f | head | grep --color=always '^\|[^/]*$' You may not want to overwrite the ...


13

ps lists bash as the running process because the bash process is blocked trying to open the fifo /tmp/in2 before spawning the cat command. Since bash is responsible for handling your redirect(> /tmp/in2), it must first open /tmp/in2 so that it can later use the dup2 system call to change the STDOUT of the cat command to the file descriptor for /tmp/in2. ...


1

You can attack this in a variety of ways. Method #1 - alias You can make an alias, php=php-5.4, and then attempt to run your script. Assuming that it relies on the current shells ability to locate how to run things, then it should pickup the alias for php instead of the php that's located under /usr/bin. Method #2 - $PATH You can override the precendence ...


5

You don't really need both grep lines, instead you might as well put: if grep -q "$input_string" "$input_string1" ; then echo "Your string has been found" else echo "Your string has not been found" fi


0

As I mentioned in the comment to f01, you should be sending SIGTERM to the child process. Here are a couple of scripts that show how to trap ^C and send a signal to a child process. First, the parent. traptest #!/bin/bash # trap test # Written by PM 2Ring 2014.10.23 myname=$(basename "$0") child=sleeploop set_trap() { sig=$1 msg="echo -e ...


2

Another (a little bit more compacted) version with sprintf: awk 'BEGIN{FS=OFS=","} $4 ~ /"RENT OUT"/ {$12=sprintf("\"REP%04i\"",++i);i=i%100}1'


3

I believe that this does what you want: $ awk 'BEGIN{FS=",";OFS=","} $4 ~ /"RENT OUT"/ {NF--;printf $0; x=x%100;x++; printf ",\"REP%04i\"\n",x;next} 1' rentals.csv "00:30:00","01:00:00","10/14/2014","RETURN","PASADENA","TX","12:30:00","sedan","","","corporate","CO01353" "01:00:00","01:30:00","10/14/2014","RENT ...


1

Parsing versions Hackey way For the first part, I'd query RPM for the particular version info like so. $ rpm -qi vim-enhanced | grep Version Version : 7.4.417 You can then parse this out like so: $ rpm -qi vim-enhanced | awk -F': ' '/Version/ {print $2}' 7.4.417 This can be captured into a variable like so: $ RPM_VERSION=$(rpm -qi vim-enhanced | ...


2

As documented in the man page, screen looks for a null title-escape-sequence. bash sends this sequence via the PROMPT_COMMAND environment variable (for example, mine defaults to printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}". To disable this feature for a particular window, I just run unset PROMPT_COMMAND from that window. Of ...


1

You need multi-threading. Have a look at GNU Parallel


3

Commands within your prompt command function alter PIPESTATUS, bash saves and restores PIPESTATUS (and $?) after your prompt command, see the description of the intended behaviour here. The trick is to save $PIPESTATUS[] (and/or $?) in the very first statement of your function, after that the original values are overwritten. function myprompt() { ...


0

Are you using symlinks ? To write reliable scripts, you should not be using "dirname $0". It relates to the $0 command, which is related to the calling command. If you are navigating across directories, use instead "readlink -f $0". basedir="$(dirname $(readlink -f $0))" $basedir/../filename {options} {parameters} & readlink -f will give you the ...



Top 50 recent answers are included