forked from nk412/optparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_head.sh
executable file
·39 lines (31 loc) · 1.23 KB
/
sample_head.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env bash
# Source the optparse.bash file ---------------------------------------------------
source optparse.bash
# Define options
optparse.define short=f long=file desc="The file to process" variable=file
optparse.define short=o long=output desc="The output file" variable=output default=head_output.txt
optparse.define short=l long=lines desc="The number of lines to head (default:5)" variable=lines default=5
optparse.define short=v long=verbose desc="Flag to set verbose mode on" variable=verbose_mode value=true default=false
# Source the output file ----------------------------------------------------------
source $( optparse.build )
if [ "$file" == "" ]; then
echo "ERROR: Please provide a file"
exit 1
fi
# Display arguments
if [ "$verbose_mode" = "true" ]; then
echo "Verbose mode ON"
echo "FILE : $file"
echo "OUTPUT: $output"
echo "LINES : $lines"
fi
# Check if input file exists
if [ "$verbose_mode" = "true" ]; then echo "Checking input file $file..." ; fi
if [ ! -f $file ]; then
echo "File does not exist"
exit 1
fi
if [ "$verbose_mode" = "true" ]; then echo "Heading first $lines lines into $output..." ; fi
cat $file | head -n $lines > $output
if [ "$verbose_mode" = "true" ]; then echo "Done."; fi
exit 0