Tag Info

New answers tagged

0

There are a few ways to go about this w/ sed. One way is a delayed read as is recommended in the accepted answer. It could also be written like: sed -e '$!N;P;/\nPointer/r file1' -e D file2 ...with a little explicit look-ahead instead of the look-behind implemented elsewhere with the hold buffer. That will inevitably have the same problem with the last ...


0

I upvoted a few of these answers, but I instinctively have the warmest feelings for @WalterA's answer. Well, I did until I created my own answer... Personally, I'd tend to update the Perl script so that it writes a descriptive log entry about the failure and sends an alert to an administrator. If the Perl script is meant to continue running, waiting for ...


1

The accepted sed answer does work for most cases but if the marker is on the last line, the command won't work as expected: it will insert the content of File1 after the marker. I initially tried with: sed '/Pointer/{r File1 N}' File2 which also works fine (as r will do its magic at the end of the cycle) but has the same problem if the marker is on the ...


0

When quoting gets too tough consider using bash functions: myfunc() { awk -F '\t' -v OFS='\t' '$1 { if($3 !~ /needle/){print;} ;}' "$1" > "$1".output } export -f myfunc parallel myfunc {} ::: *.txt


1

In general I would use a Makefile (command make) and try to map your diagram to makefile rules. f1 f2 : f0 command < f0 > f1 2>f2 To have repetitive/cyclic commands, we need to define a iteration policy. With: SHELL=/bin/bash a.out : accumulator cat accumulator <(date) > a.out cp a.out accumulator accumulator: touch ...


2

This looks like a quoting issue. Perhaps the easiest way to debug this is to pass the option --dry-run to parallel: $ parallel --dry-run "awk -F '\t' -v OFS='\t' '$1 { if($3 !~ /needle/){print;} ;}' {} > {}.output" ::: in awk -F '\t' -v OFS='\t' ' { if( ~ /needle/){print;} ;}' in > in.output There you can see that your variables $1 and $3 have ...


5

You can use a FIFO for this, created with mkfifo. Note however that its very easy to accidentally create a deadlock. Let me explain that—take your hypothetical "circular" example. You feed a command's output to its input. There are at least two ways this might deadlock: The command has an output buffer. It's partially filled, but hasn't been flushed ...


5

Circular I/O Loop Implemented with tail -f This implements a circular I/O loop: $ echo 1 >file $ tail -f file | while read n; do echo $((n+1)); sleep 1; done | tee -a file 2 3 4 5 6 7 [..snip...] This implements the circular input/output loop using the sine algorithm that you mentioned: $ echo 1 >file $ tail -f file | while read n; do echo ...


2

This is slightly beyond the powers of what shell aliases provide (assuming bash). You could define a function: function tail() { if [ "$1" == '-f' ]; then shift less +F "$@" else command tail "$@" fi } When you type tail, this will now refer to the function defined above, which checks its first argument, if any, for ...


0

The while loop is really not a good idea. There's no escape there - it just runs forever - statically. Any number of things could change in the environment and it will not be affected - and this could be bad. For example, if the shell executable responsible for that while loop is upgraded the kernel will not be able to release the disk space for the old ...


4

The other answers, about using inotify, are correct, but not an answer to this question. A process supervisor, such as supervisord, upstart, or runit, is designed for exactly the problem of watching and restarting a service if it crashes. Your distro probably comes with a process supervisor built in.


3

while true is fine as a general-purpose "loop forever" construction. As other answers say, the body of the loop shouldn't be empty, or become empty by virtue of the command inside the loop not working. If you're using Linux, you may want to use a command like inotifywait, which makes the while loop much simpler: while inotifywait -qqe modify "$DIRECTORY" ...


2

env | grep XDG_CURRENT_DESKTOP returns the desktop environment currently in use. In my case, I see LXDE which is the desktop environment Lubuntu currently uses. echo $DESKTOP_SESSION returns Lubuntu.


4

Move the while 1 into the perl script (Following @roaima suggestion) #!/usr/bin/perl use Linux::Inotify2; my $inotify = new Linux::Inotify2 or die "unable to inotify: $!"; $inotify->watch ("Dir", IN_MODIFY, ## or in_{acess,create,open, etc...} sub { my $e = shift; my $name = $e->fullname; ## whatever print "$name was ...


3

When your perl script is intended to keep on running all the time, why use the while construction ? When the perl fails in view of some serious problem, the new perl script started by the while might crash just as hard. Again-and-again-and-so-on. if you really want your perl started over, consider crontab and a script that first checks for running instances. ...


2

In general there is no problem using while true since it is a tiny test which is only executed after the perl script is terminated. Keep in mind that depending on which Linux/Unix variant you are using, the script might get terminated at logging off. In such case consider using the loop in a script and call it with nohup and put it in the background i.e. ...


10

That depends on how fast the perl script returns. If it returns quickly, you might want to insert a small pause between executions to avoid CPU load, eg: while true do /someperlscript.pl sleep 1 done This will also prevent a CPU hog if the script is not found or crashes immediately. The loop might also better be implemented in the perl script itself ...


1

chmod -f 777 file.txt || true As it's an OR, if one of the statements returns true, then the the return is true. This results in an exit status of zero.


2

The semicolon ; is the sequencing operator. So in command1 & command2 ; command3 or equivalently command1 & command2 command3 command3 will run after command2 while command1 may still be running. The command wait will wait for all background processes (command1 in your example) to complete.


1

The [[ construct doesn't exist in all sh variants. It's a ksh thing that was adopted by bash and zsh. FreeBSD's sh is an ash derivative and doesn't support [[. If you want to use [[ in a script, use a shebang line that calls a shell that supports [[. You can install bash or ksh93 or mksh as a package on FreeBSD; all of them support [[. Packages are ...


2

rm's stdin (where it reads the prompt answer from) is /dev/null (set by GNU xargs, some other xargs implementations would keep it as the pipe from ls). Your sh is getting many arguments at once, but you're only processing one ($1). Also note that the newline character is as valid as any in a file name which is why you generally can't process the output of ...


5

[[ is a bashism. /bin/sh is not guaranted to be the Bourne Again shell. Even on Linux operating systems, it could be the Debian Almquist shell, or the Policy-Compliant Ordinary shell. On the BSDs, it is not the Bourne Again shell out of the box because on the BSDs the Bourne Again shell is an optional add-on to the operating system proper. It's in the ...


2

The [[ syntax is a ksh and bash thing and is not present in all shells. Your FreeBSD default shell is probably sh (or bash acting like sh), not bash. The equivalent syntax that should work on all shells is: case $1 in *[/\\] ) echo "Yes";; esac


2

You don't say what shell are you using. From the behaviour you are describing it's likely zsh. If you have a look in its man page you would notice how redirections are handled. Note that a pipe is an implicit redirection; thus cat bar | sort <foo is equivalent to cat bar foo | sort (note the order of the inputs). Otherwise, regular ...


2

Your understanding is not quite correct. In a | b the stdout output of process a connected through a pipe to stdin of process b. The problem with your code is that with an additional redirection from somefile to process b you will use two different methods at the same time to connect to stdin of process b. Don't do that! The question is; what do you try to ...


-2

Try this: IFS=$(echo -en "\n\b") touch "$yourfile"


0

Try this: $ spam="foo bar" $ touch "$spam" $ ls -l -rw-rw-r-- 1 user user 0 Mar 29 05:14 foo bar


7

export foo=bar is not supported by the Bourne shell. That was introduced by ksh. In the Bourne shell, you'd do: foo=bar export foo or: foo=bar; export foo or with set -k: export foo foo=bar Now, the behaviour of: export foo=bar varies from shell to shell. The problem is that assignments and simple command arguments are parsed and interpreted ...


0

The GNU version of date that you're used to on Linux supports a lot more date format than the version of date on most other Unix variants. It also has many options that aren't present on other Unix variants. The only standard usage of date is to display the current date according to a format specified with a +… argument (and also a way for the system ...


1

i have the same problem, this solution work for me: Boot with a live cd (i use debían 7.6) in live mode In live terminal, get root access running the command: passwd root Mount the disk of the partition of you crash distro Go to this disk and run this command: cat etc/shells With nano etc/passwd remove the fish references and put a valid console name of ...


17

It is not a bashism but a POSIX compliant syntax. It actually started as a kshism quite a long time ago and was later adopted by almost all Bourne syntax based shells. The only notorious exception is /bin/sh on Solaris 10 and older which sticks to the legacy Bourne shell syntax. Hopefully, Solaris 11 uses a POSIX compliant shell as /bin/sh. By the way, ...


1

I don't have a bsd box to test on, but it appears that you need to use this form: date -f "some format" "$datum" "+%Y-%m-%d" and you have to specify the format of the incoming datum so it can be parsed.


2

Try this: [/\\]$ That is, a forward slash or a backward slash, followed by end-of-string. There are many "flavours" of regular expressions: what tool are you using?


1

Try this: $ date -d "$datum" +"%Y-%m-%d" >/dev/null 2>&1 && echo "OK" || echo "ERROR" If the date command returns exit code 0 then OK will be printed, otherwise ERROR will be printed.


0

I am doing introductory Linux as also. I see where it means total number of arguments. You can use it like this: #!/bin/bash if [ $# = 2 ] then echo "Your name is $1 $2" else echo "You need two arguments" fi


1

Suppose the following bash : #!/bin/bash echo $# And you run such as : ./arg.sh g jt t uu It return 4, 4 is number of argument that you pass to your shell. It's very good to investigate your parameters of your shell script. Supppose i have the following usage of program : --value PATTERN -o PS_COMMAND_OPTIONS Then I can investigate such as the ...


3

In any POSIX-compatible shell you can do: case $line in (*"$PWD"*) # whatever your then block had ;;esac This works in bash, dash, and just about any other shell you can name. It can also be used to handle multiple possibilities easily. For example: case $line in (*"$PWD"*) echo \$PWD match\! ;; (*"$OLDPWD"*) echo \$OLDPWD match\! ;; (*) ...


2

You can pipe output via following script | while read a b ; do [ $a -gt 27 ] && echo "$a $b" ; done or | while read ; do [ ${REPLY% *} -gt 27 ] && echo "$REPLY" ; done but easyest through awk | awk '$1 > 27'


-1

The following will work bash, zsh, ksh and dash. line="I'm in a pickle, where is $HOME" whereami=$(echo $line | awk -v pwd=$PWD '$0 ~ pwd') if [ -z "$whereami" ];then echo "I'm on the wrong path" else echo "I'm on the right path" fi


3

I'd recommend screen for this kind of situation. (Or run the command in a different window if that's feasible for you.) To start a detached session, just enter screen. Then type your command but without the & to background it. Use Ctrla then d to detach the running session. Use screen -ls to list existing sessions and screen -r to reattach.


2

Yes, recent versions of bash can do this: $ pwd /home/terdon $ line="I'm in /home/terdon" $ [[ "$line" =~ "$PWD"$ ]] && echo yes yes The same syntax works in zsh and ksh but not in dash. As far as I know, dash has no such capabilities. Note that your regex is checking whether the variable $line ends with $PWD. To check if $PWD matches anywhere in ...


5

The rm command refuses to delete the directory by the '.' name. If you instead use the full path name it should delete the directory recursively. It is also possible to delete the directory if it is the current directory. [testuser@testhost] /tmp$ mkdir ff [testuser@testhost] /tmp$ cd ff [testuser@testhost] /tmp/ff$ touch a b c [testuser@testhost] ...


0

You cannot remove the current directory because then the current directory would become invalid. First, change out of the directory you want to remove (e.g. cd ..) and then remove the desired directory using its full or relative pathname from outside.


0

You can reference a function's arguments as numbered expansions in order of their appearance on the command-line when the function was called. If you wish to require that an argument is present in order for a function to operate, you can do this via an intrinsic failure-reference to the argument in question via parameter-expansion. echo1()( printf %s\\n ...


-2

For a regular file you can do: while [ -s /path/to/file ] && exec </path to file do dd bs=1k count=1 dd bs=1k of=/path/to/file done


1

I want to print the contents of the file and then delete printed contents from the file lpr file && truncate -s0 file


2

In a command substitution delimited with $(…), what's inside the parentheses is parsed in the same way as a toplevel command (except in a few corner cases involving unbalanced closing parentheses). What's inside the parentheses is an ordinary shell snippet, there's no additional backslash expansion being done. The command echo '\\' prints three characters: ...


-1

A function, like an external command, receives a list of parameters. There's no way to define a function that intrinsically requires a certain number of parameters. The code of the function can check the number of parameters and their value and emit error messages if it wishes. The number of parameters passed to the current function is available in the ...


6

The character . is only excluded from wildcard matching when it's the first character of the file name and it would be matched by a wildcard. In the pattern .*, the * matches strings beginning with ., so .* includes .. (as well as ., with * matching the empty string). This is a straightforward consequence of the pattern matching rules, annoying though it may ...


1

I'd think you could use ls -A instead, specifically: chown -R username:groupname $(ls -A | grep '^\.') This does what you'd expect .* to do, match all files in the current directory that begin with a ., excluding . and ... But note this won't behave identically to a bash glob if you need it to match funky file names, like files with spaces in them.



Top 50 recent answers are included