In a unix shell, how to get yesterday’s date into a variable?
dt=$(date –date yesterday “+%a %d/%m/%Y”) echo $dt
dt=$(date –date yesterday “+%a %d/%m/%Y”) echo $dt
In bash, read can do it: read -n1 ans
If you’re using GNU find, then find path -printf “%f\n” will just print the file name and exclude the path.
If you want test.log, test2.log, and file2 then: find . -type f If you do not want file2 then: find . -maxdepth 1 -type f
ipcs -mb | tail +4 | awk ‘{ sum += $7 } END { print sum }’ Or without tail: ipcs -mb | awk ‘NR > 3 { sum += $7 } END { print sum }’ Using awk with bc to have arbitrary long results (credits to Jouni K.): ipcs -mb | awk ‘NR … Read more
Below is the fixed code: #!/bin/ksh safeRunCommand() { typeset cmnd=”$*” typeset ret_code echo cmnd=$cmnd eval $cmnd ret_code=$? if [ $ret_code != 0 ]; then printf “Error: [%d] when executing command: ‘$cmnd'” $ret_code exit $ret_code fi } command=”ls -l | grep p” safeRunCommand “$command” Now if you look into this code, the few things that I … Read more
Commands inherit their standard input from the process that starts them. In your case, your script provides its standard input for each command that it runs. A simple example script: #!/bin/bash cat > foo.txt Piping data into your shell script causes cat to read that data, since cat inherits its standard input from your script. … Read more
In bash: path=/this/is/could/be/any/path/abc.txt If your path has spaces in it, wrap it in ” path=”/this/is/could/be/any/path/a b c.txt” Then to extract the path, use the basename function file=$(basename “$path”) or file=${path##*/}
install -m 777 /dev/null filename.txt
The difference between Kornshell and Bash are minimal. There are certain advantages one has over the other, but the differences are tiny: BASH is much easier to set a prompt that displays the current directory. To do the same in Kornshell is hackish. Kornshell has associative arrays and BASH doesn’t. Now, the last time I … Read more