Shell Scripts
This page contains some of the shell scripts and some of the shell utils that are useful in regular work.
How to keep files safe from accidental overwriting with noclobber under BASH shell?
This happens many a times, user may accidentally use '>' operator and override original file. Or sometimes you may use '>' instead of '>>'. In such a cases, user can use noclobber to ask before doing any action.
Set noclobber:
Now redirect some result to an already existing file called as "temp"
Then bash will give an error message saying:
temp: cannot overwrite exixting file
Rather than setting each time use the below statement to place permenantly the noclobber setting statement as below:
echo "set -o noclobber" >> ~.bashrc
Unset noclobber
Temporarily turn off noclobber
The above command will allow user to overwrite the existing "temp" file.
">|" operator is used to force the file to be overwritten.
Read one character at a time
The read built-in can read one character at a time and syntax is as follows:
read -n 1 c
echo $c
You can setup the while loop as follows:
#!/bin/bash
# data file
INPUT=/path/to/input.txt
# while loop
while IFS= read -r -n1 char
do
# display one character at a time
echo "$char"
done < "$INPUT"
Example: Letter frequency counter shell script
#!/bin/bash
INPUT="$1"
# counter
a=0
b=0
cc=0
# Make sure file name supplied
[ $# -eq 0 ] && { echo "Usage: $0 filename"; exit 1; }
# Make sure file exits else die
[ ! -f $INPUT ] && { echo "$0: file $INPUT not found."; exit 2; }
# the while loop, read one char at a time
while IFS= read -r -n1 c
do
# counter letter a, b, c
[ "$c" == "a" ] && (( a++ ))
[ "$c" == "b" ] && (( b++ ))
[ "$c" == "c" ] && (( cc++ ))
done < "$INPUT"
echo "Letter counter stats:"
echo "a = $a"
echo "b = $b"
echo "c = $cc"
Linux / UNIX: Restrict Access To A Given Command
How do I restrict access to a given command for instance /opt/apps/start, to authorized users only under Linux / UNIX / BSD operating system?
You need to use traditional Unix groups concept to enhance security including restricted access to a given command.
Step # 1: Create and Maintain a Group For All Authorized Users
Create a group named appsonly:
# groupadd appsonlyAdd all authorized users to appsonly:
# usermod -aG {groupName} {userName}
# usermod -aG appsonly tom
# usermod -aG appsonly jerry
# id jerryWhere,
- -a : Add the user to the supplemental group(s) i.e. appends the user to the current supplementary group list.
- -G : A list of supplementary groups which the user is also a member of.
Step #2: Restrict Access
Now a group of user had been created. Next, use the chgrp command to change the group of /opt/apps/start to appsonly group:
# chgrp {groupName} {/path/to/command}
# chgrp appsonly /opt/apps/start
Disable the file permission for others
Finally, use the chmod command to change file permission as follows:
# chmod 750 /path/to/command
# chmod 750 /opt/apps/startYou can also apply permissions to directory (this will disable ls command access to others) :
# chgrp appsonly /opt/apps
# chmod 0640 /opt/apps
Step # 3: Test It
su to tom, enter:
# su - tom
$ id
$ /opt/apps/start
$ exitsu to vivek (not a member of appsonly group), enter:
# su - vivek
$ id
$ /opt/apps/startSample outputs:
bash: /opt/apps/start: Permission denied
Comments (0)
You don't have permission to comment on this page.