Tag Info

Hot answers tagged

18

{ cat sample1.txt; tail -n +4 sample2.txt; tail -n +4 sample3.txt; } > out.txt


10

A few pieces of documentation will help to explain this. From the POSIX standards document for the shell: The following variables shall affect the execution of the shell: PS1: Each time an interactive shell is ready to read a command, the value of this variable shall be subjected to parameter expansion and written to standard error. ... ...


8

The only reference I can find to the special parameter $_ in POSIX is in the rationale section on Shell Variables. This excerpt implies that it was used by a variety of shells, but not in a standard way by all and was omitted intentionally: _ (Underscore.) While underscore is historical practice, its overloaded usage in the KornShell is confusing, ...


8

The weird string "@(#)" is actually used by the ancient SCCS version control system. Specifically, the what command would look through a file (binary or text) and find ASCII-Nul-terminated strings that started with "@(#)", and print that string out. That allowed you to embed printable ASCII version numbers in ".o" files and ultimately executables, so you ...


7

The LINE parameter isn't quoted so wordsplitting happens upon the expansion of $LINE in echo $LINE and by the time awk receives any input, you have 7 words(as seen by the shell) all separated by a single space. You want echo to output it as one word(again, as seen by the shell) so the whitespace in your line isn't mangled before awk can process it. That is ...


7

sed '4,${/^---/d;/^Date/d;}' sample1.txt sample2.txt sample3.txt > out.txt


7

The grep with awk is redundant: hasys -display | awk '/Shutdown/ { printf "%s ", $1 }'


7

With awk: awk '{ print $(NF-1) }' NF is the number of fields -- all that happens here is that one is subtracted from the total field length to get the penultimate field. With perl: perl -lane 'print $F[-2]' An array containing the fields is created as @F (that's what -a does), and we get the value of the second last field (with index -2). Using sed ...


6

ksh 93 has a nameref command that let's you create "aliases" to variables: var1EMI=a var2EMI=b for v in var1 var2; do nameref var=${v}EMI echo "${v}EMI is $var" done var1EMI is a var2EMI is b For ksh88, you may be forced to use eval: replace nameref var=${v}EMI with eval var=\$${v}EMI


6

With GNU sort and GNU du (which it appears you have, since you state you are using du's -h option): du -sh -- * | sort -rh # Files and directories, or du -sh -- */ | sort -rh # Directories only The output looks something like this: 22G foo/ 21G bar/ 5.4G baz/ 2.1G qux/ 1021M wibble/ 4.0K wobble/


6

awk -Fy -v OFS=y '{gsub(",","u",$2); print}' file


6

Try "./a.sh" when trying to execute it. It needs to know where the file is at. The './' tells it to look in the current directory.


5

If you're sure that end_time is always greater than start_time, you can use Perl like so: export start_time=06:07:25 export end_time=07:02:08 perl -e ' ($h1,$m1,$s1) = split /:/,$ENV{start_time}; ($h2,$m2,$s2) = split /:/,$ENV{end_time}; $delta_h = $h2 - $h1; $delta_m = $m2 - $m1; if( $delta_m < 0 ) { $delta_m = $delta_h-- * 60 + $m2 ...


5

There are 2 redirects there. The last bit, 2>&1 is actually merging STDERR in with STDOUT. This looks to me like someone set this up to log output to the doit.log file but then wanted to disable it. Chaining redirects in this manner, basically negates the earlier ones, so that only the output, if there is any, will get directed to the last redirected ...


5

You could put the read and your case in a while loop and break out of it when the condition is satisfied: while : ; do echo "yes or no?" read ans case $ans in [yY]*) echo "yes" break ;; [nN]*) echo "no" break ;; *) echo "yes or no only" ;; esac done The while : ; do ... ...


4

You can do it in two steps: newdir="$(find . -type d | grep "${4:0:4}.*${4:4:2}.*${4:6:2}")" if [ -z "$newdir" ];then # error out here else cd "$newdir" fi ...


4

Like Lawrence has mentioned, you can use cp -v to enable "verbose" mode, which displays the files you copy. Something else that might be useful is cp -v > foo which will output the list of files to a file called foo. This is useful if you're going to copy a lot of files and you want to be able to review the list later.


4

There are 3 main implementations of ksh the original one from David Korn (AT&T ksh), with two main branches: ksh88 and ksh93 (and for ksh93, many version with new features added for each). pdksh, the public domain version (a free reimplementation of ksh88 with which it is mostly compatible) which is the base upon which is built the sh on some BSDs like ...


4

Assuming all the filenames in the current directory follow that pattern, with GNU ls: ls -rv | awk -F_ '++n[$1]>2' | xargs rm You can omit the | xargs rm if you want to check what it would remove first. That assumes the part in {...} doesn't contain space, newline, single quote, double quote, backslash, tab or underscore characters. The key is the -v ...


4

hasys -display | awk '/Shutdown/ {print $1}' | paste -sd ' ' - Contrary to @devnull's and @jasonwryan's, this one doesn't add a trailing space and terminates the line. paste -sd<sep> - is the generic idiom to join lines into a <sep> separated list.


4

You're missing the do keyword: while ...; do ... done


4

If you don't background the tail command in your script, the shell will wait for it to exit, which will never happen. If you have other work to do, after which you want to kill the tail command, you can tail -f logfile & tailpid=$! ...do some other stuff... kill $tailpid ...carry on...


4

Try: #!/bin/bash id touch script-run-user.file sudo -u appuser 'ksh' <<EOF # add list of cmds to execute id touch appuser.file EOF Edit: Just as an update, check out Here Documents. EOF = "End Of File", the name is arbitrary.


4

stty and older versions of who am i will issue error messages when they're not connected to a tty device. stty checks stdin (fd 0); I don't know what file descriptor who checks. To avoid getting those error messages, the usual workaround has been to use the -t option of test (more commonly known as [) to check if the shell is connected to a tty. if [ -t 0 ] ...


4

The \ character escapes the following (special) character. In this case, it escapes the $, which we usually use to dereference a variable. When the shell evaluates a variable assignment, it first expands the right-hand-side of the expression. Without the \ before $PWD, the shell expands $PWD and assigns the result to PS1. However, with the \, the shell ...


3

That's because your shell starts another shell to run your script. The second shell changes its working directory and then exits to the first shell, which never changed its directory. To make your current shell run the script, call it this way: . scriptname (notice the dot). Of course, you'll have to take out that exit 0. You can also write a shell ...


3

You can do this in two commands: find A -name "*.log" -size -10485760c -exec cp {} B/ + find A -name "*.log" -size +10485760c -exec cp -i {} B/ + The first finds files smaller than 10485760 and copies indiscriminately. The second finds files larger than 10485760 and copies interactively, prompting for each copy. Put them into a shell script or function ...


3

As hinted at in this question, the answer is setting up a trap on the ERR signal - basically set -e corresponds to trap 'exit' ERR. So to e.g. log errors but continue execution, use trap 'logger -t myscriptname "Command $BASH_COMMAND exited with code $?"' ERR In bash, the variable $BASH_COMMAND contains the offending command. $? contains the exit code of ...


3

Try using: awk 'BEGIN { RS="[" } { if (index($0, "cics]") == 1) print; }' To filter your input. It will not include the [ characters. I would suggest using the output of awk directly rather than using a temp file.


3

There is a way in bash 4.3+, which probably comes from ksh: echo_idx_array () # array index { local -n array=$1 # add nameref attribute local idx=$2 echo "${array[idx]}" } $ names=(one two three four) $ echo_idx_array names 2 three $ days=([monday]=eggs [tuesday]=bread [sunday]=jam) # associative array $ echo_idx_array days sunday jam ...



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