-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_file_chmod
executable file
·215 lines (193 loc) · 5.35 KB
/
get_file_chmod
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
#
# @author Zeus Intuivo <[email protected]>
#
# Bash: Detect pipe/file input in a shell script
# REF: https://gist.github.com/davejamesmiller/1966557
# How to detect whether input is from keyboard, a file, or another process.
# Useful for writing a script that can read from standard input, or prompt the
# user for input if there is none.
# Source: http://www.linuxquestions.org/questions/linux-software-2/bash-scripting-pipe-input-to-script-vs.-1-570945/
PIPED="";
COLORED="";
COUNTER=0;
# ag -i filefoo /bar/
# ag [FILE-TYPE] [OPTIONS] PATTERN [PATH]
# ack [OPTION]... PATTERN [FILES OR DIRECTORIES]
# sift [OPTIONS] PATTERN [FILE|PATH|tcp://HOST:PORT]...
# sift [OPTIONS] [-e PATTERN | -f FILE] [FILE|PATH|tcp://HOST:PORT]...
# sift [OPTIONS] --targets [FILE|PATH]...
# grep [-abcdDEFGHhIiJLlmnOopqRSsUVvwxZ] [-A num] [-B num] [-C[num]] [-e pattern] [-f file]
# [--binary-files=value] [--color[=when]] [--colour[=when]] [--context[=num]]
# [--label] [--line-buffered] [--null] [pattern] [file ...]
# In Bash you can also use test -t to check for a terminal:
if [ -t 0 ]; then
# Terminal input (keyboard) - interactive
PIPED=""
else
# File or pipe input - non-interactive
PIPED="YES"
# REF: http://stackoverflow.com/questions/2746553/bash-script-read-values-from-stdin-pipe
# read PIPED #TOOD Works on mac osx, pending to test in linux and windows
PIPED=''
# REF: https://stackoverflow.com/questions/7314044/use-bash-to-read-line-by-line-and-keep-space
# REF: http://www.unix.com/shell-programming-and-scripting/58611-resetting-ifs-variable.html
OLDIFS=$IFS # resetting IFS variable
IFS='' # to read line by line and keep space
IFS= read -r ONEPIPE # to avoid interpretation of backslashes.
PIPED="${ONEPIPE}"
[ ! -z "$PIPED" ] && COUNTER=$((COUNTER+1))
while read ONEPIPE
do
PIPED="${PIPED}
${ONEPIPE}"
COUNTER=$((COUNTER+1))
done
if [ $COUNTER -eq 0 ]; then
IFS=$OLDIFS
exit
fi
#action="${ONEPIPE/ /⃝}" # replace value inside string substitution expresion bash
# TEST: echo "COUNTER: ${COUNTER}";echo "PIDED: ${PIPED}"; exit 0;
IFS=$OLDIFS
fi
# Piped Input
#if [ ! -z "$PIPED" ]
# then
# echo "this is pipe..die "
# exit 0;
#fi
# NOT Piped Input
#if [ -z "$PIPED" ]
# then
# echo "this NOT pipe..die "
# exit 0;
#fi
#echo "PIPED:$PIPED";
#exit;
# ALTERNATIVE:
#if readlink /proc/$$/fd/0 | grep -q "^pipe:"; then
# Pipe input (echo abc | myscript)
# PIPED="YES"
#elif file $( readlink /proc/$$/fd/0 ) | grep -q "character special"; then
# Terminal input (keyboard)
# PIPED=""
#else
# File input (myscript < file.txt)
# PIPED=""
#fi
# CURRENT SCRIPT EXECUTING
THISSCRIPTNAME=`basename "$0"`
# PIPED ? - END
# ARGUMENTS ? - Start
# check to see if I used a message, then it will behave as a put
# bash shell script check input argument
FILENAME=""
VERBOSE=""
# Yes argument, not piped
if [ ! -z "$1" ] && [ -z "$PIPED" ]
then
{
if [[ "$1" == "-v" ]] ;
then
VERBOSE="ON"
else
FILENAME="$1";
fi
}
fi
# Yes argument, yes piped
if [ ! -z "$1" ] && [ ! -z "$PIPED" ]
then
{
if [[ "$1" == "-v" ]] ;
then
VERBOSE="ON"
fi
FILENAME="${PIPED}";
}
fi
# Not argument, yes piped
if [ -z "$1" ] && [ ! -z "$PIPED" ]
then
{
FILENAME="${PIPED}";
}
fi
# Not argument, not Piped
if [ -z "$1" ] && [ -z "$PIPED" ]
then
{
echo " "
echo " 'Get the chmod numerical value for a file' REF: http://unix.stackexchange.com/questions/46915/get-the-chmod-numerical-value-for-a-file"
echo "Missing 1st argument "
echo " "
echo "Sample Usage: - expects one argument *required"
echo " "
echo " ${THISSCRIPTNAME} filename [-v] for verbose"
echo " "
echo "Pick one: "
echo " "
ls
exit 1;
}
fi
# Yes argument, not piped
if [ ! -z "$2" ]
then
{
if [[ "$2" == "-v" ]] ;
then
VERBOSE="ON"
fi
}
fi
# ARGUMENTS ? - END
# WHICH SYSTEM AND TAKE ACTION ? - Start
# check operation systems
(
if [[ "$(uname)" == "Darwin" ]] ; then
# Do something under Mac OS X platform
while read -r ONE_FILENAME; do
# if not empty
PERMISIONS=""
if [ ! -z "${ONE_FILENAME}" ] ; then
PERMISIONS=$(stat -f "%OLp" "${ONE_FILENAME}")
fi
if [[ "${VERBOSE}" == "ON" ]] ;
then
{
echo "${PERMISIONS} ${ONE_FILENAME}"
}
else
{
echo "${PERMISIONS}"
}
fi
done <<< "${FILENAME}"
elif [[ "$(expr substr $(uname -s) 1 5)" == "Linux" ]] ; then
# Do something under GNU/Linux platform
while read -r ONE_FILENAME; do
# if not empty
PERMISIONS=""
if [ ! -z "${ONE_FILENAME}" ] ; then
PERMISIONS=$(stat --format '%a' "${ONE_FILENAME}")
fi
if [[ "${VERBOSE}" == "ON" ]] ;
then
{
echo "${PERMISIONS} ${ONE_FILENAME}"
}
else
{
echo "${PERMISIONS}"
}
fi
done <<< "${FILENAME}"
elif [[ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]] || [[ "$(expr substr $(uname -s) 1 10)" == "MINGW64_NT" ]] ; then
# Do something under Windows NT platform
echo "Not learned yet. nothing"
# nothing here
fi
)
# WHICH SYSTEM AND TAKE ACTION ? - END