Search Results
| Search type | Search syntax |
|---|---|
| Tags | [tag] |
| Exact | "words here" |
| Author |
user:1234 user:me (yours) |
| Score |
score:3 (3+) score:0 (none) |
| Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
| Views | views:250 |
| Code | code:"if (foo != bar)" |
| Sections |
title:apples body:"apples oranges" |
| URL | url:"*.example.com" |
| Saves | in:saves |
| Status |
closed:yes duplicate:no migrated:no wiki:no |
| Types |
is:question is:answer |
| Exclude |
-[tag] -apples |
| For more details on advanced search visit our help page | |
Results tagged with shell-script
Search options not deleted
user 22565
Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.). Check scripts with https://shellcheck.net before posting.
3
votes
What is exit $? in shell script?
The intention with exit $? is generally to exit the shell with the same exit status as the previous command (whose $? is the shell's representation). However it has a few issues:
Quotes were omitted …
6
votes
How to expand IP Address in dash format?
With perls Net::IP module (libnet-ip-perl package in Debian based systems):
perl -MNet::IP -lne '
print $an_empty_line unless $. == 1;
my $ip = Net::IP->new($_);
do {print $ip->ip} while (++$ip) …
7
votes
How to print first 20 lines from file using cut command
With some cut implementations (including GNU's and busybox), you can do:
cut -d'
' -f1-20 < your-file
(that's on two lines, so as to have a newline character inside the '...' quotes; with shells that …
1
vote
Check if the file lies in the specified interval in unix
I'd use perl:
perl -MText::CSV -le '
$csv = Text::CSV->new ({ binary => 1});
$csv->getline(STDIN); # skip input header
$csv->print(*STDOUT, ["Scorecard_Name", "Status"]);
while($row = $csv->g …
1
vote
How to generate random single digit number using shell
POSIXly:
awk 'BEGIN{srand(); print int(rand()*10)}'
Beware that with most awk implementations, that will yield the same number if called twice within the same second.
45
votes
Get the date of last month's last day in a shell script
With GNU date:
$ date +%d%b%Y
16Aug2015
$ date -d "$(date +%Y-%m-01) -1 day" +%d%b%Y
31Jul2015
Some shells have built-in support for date manipulation:
With ksh93:
$ printf "%(%d%b%Y)T\n" "1st da …
0
votes
How to turn verbose mode on and off using a case statement in UNIX
There are false and true commands that are handy to express booleans in shells. For verbosity, a common approach is to use a log level where each occurrence of -v increases the log level and -q decre …
2
votes
In shell scripting, what's the general rule of using blank space
In the shell language syntax, blanks¹ are used to delimit arguments of simple commands (such as your expr invocation).
Where in other languages, you'd do things like:
exec("ls","-lA","path/to/director …
0
votes
how to use strings in a file as a variable
Double quotes may not be the best choice of quotes there as within them, the $, ", backslash and backtick characters are still special.
I'd use single quote characters instead (key='name') as then th …
3
votes
I want to list all files(with absolute path) from a given directory(given absolute path) in ...
With zsh:
print -rC1 -- ${PWD%/}/path_to_dir/*(NDom.) # regular files only
print -rC1 -- ${PWD%/}/path_to_dir/*(NDom^/) # any type of file except
# direct …
2
votes
Script to get string and compare the output from the array and check if they are equal
Your script is not using sh syntax. If anything that looks more like a mix of ksh and zsh syntax. For basic syntax check, you may want to use shellcheck (available online or as a standalone utility yo …
2
votes
Need to get the latest file with date timestamp for each month
On the excerpt you gave, this:
ls -r | POSIXLY_CORRECT=1 awk '
match($0,"20[0-9]{6}") && !n[substr($0,RSTART,6)]++'
would work. The POSIXLY_CORRECT=1 is only needed with GNU awk which doesn't rec …
1
vote
Concatenation of two fields of two files
paste -d '\0' t1.txt t2.txt > t3.txt
No it doesn't include NUL characters between the two files.
Yes, it is standard (POSIX) and portable, and the most efficient you can get.
13
votes
Accepted
Remove all lines except D
cat Test.txt Test2.txt Test3.txt | LC_ALL=C grep '^D' > newfile.txt
Or:
for file in Test.txt Test2.txt Test3.txt; do
LC_ALL=C grep '^D' < "$file"
done > newfile.txt
Or if your grep like GNU gre …
0
votes
Shell script to send a mail once every 20 days
You could always do (assuming GNU date and bash/ksh93/zsh):
o=$(date +%::z)
o=${o:0:1}$((10#${o:1:2} * 3600 + 10#${o:4:2} * 60 + 10#${o:7}))
[ "$((($(date +%s) $o) /(24*60*60) % 20))" -eq 0 ] || exit …