sed not working as intended in bash script - bash

I am trying to replace some strings in a file with sed and I'm getting a strange error.
Here is the script...
#!/bin/bash
TEMPLATE=/etc/nginx/site-template
if [ -f $TEMPLATE ]; then
echo -n
else
echo "Template $TEMPLATE not found."
exit
fi
function usagehelp {
echo "Usage: DOMAIN SITEDIR"
echo "Example: createsite.sh test.com /var/www/sites/site"
}
if [ -z $1 ]; then
usagehelp
exit
fi
if [ -z $2 ]; then
usagehelp
exit
fi
SDOMAIN=$1
SDIR=$2
if [ -f /etc/nginx/sites-enabled/$SDOMAIN ]; then
echo "Site already exists!"
exit
fi
SCONFIG=/etc/nginx/sites-enabled/$SDOMAIN
cp $TEMPLATE $SCONFIG
sed -i -e "s/%DOMAIN%/$SDOMAIN/g" $SCONFIG
sed -i -e "s/%SITEDIR%/$SDIR/g" $SCONFIG
mkdir $SDIR
chown john:www-data $SDIR
chmod a-rwx,u+rwx,g+rx $SDIR
if nginx -t; then
nginx -s reload
echo "nginx reloaded with new site"
fi
Running it produces the following output, and I'm not quite sure why. I really need an extra set of eyes...
sed: -e expression #1, char 14: unknown option to `s'
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx reloaded with new site

In the case of %SITEDIR%, if your $SDIR contains a directory name, then the first / in the name will be seen as the end of the s/// command, rather than as data.
The bad answer is to try to pick a sigil which won't likely be in the filename -- for instance, if you don't expect the # sign to be in a filename, use:
sed -i -e "s#%SITEDIR%#$SDIR#g"
The good answer is to use a completely different tool -- awk, perl, etc. There's an awk script which does this safely in BashFAQ #21.

Related

(Ubuntu bash script) Setting rights from a config txt

I am a beginner and trying to write a script that takes a config file (example below) and sets the rights for the users, if that user or group doesn´t exist, they get added.
For every line in the file, I am cutting out the user or the group and check if they exist.
Right now I only check for users.
#!/bin/bash
function SetRights()
{
if [[ $# -eq 1 && -f $1 ]]
then
for line in $1
do
var1=$(cut -d: -f2 $line)
var2=$(cat /etc/passwd | grep $var1 | wc -l)
if [[ $var2 -eq 0 ]]
then
sudo useradd $var1
else
setfacl -m $line
fi
done
else
echo Enter the correct path of the configuration file.
fi
}
SetRights $1
The config file looks like this:
u:TestUser:- /home/temp
g:TestGroup:rw /home/temp/testFolder
u:TestUser2:r /home/temp/1234.txt
The output:
grep: TestGroup: No such file or directory
grep: TestUser: No such file or directory
"The useradd help menu"
If you could give me a hint what I should look for in my research, I would be very grateful.
Is it possible to reset var1 and var2? Using unset didn´t work for me and I couldn´t find variables could only be set once.
It's not clear how you are looping over the contents of the file -- if $1 contains the file name, you should not be seeing the errors you report.
But anyway, here is a refactored version which hopefully avoids your problems.
# Avoid Bash-only syntax for function definition
SetRights() {
# Indent function body
# Properly quote "$1"
if [[ $# -eq 1 && -f "$1" ]]
then
# Read lines in file
while read -r acl file
do
# Parse out user
user=${acl#*:}
user=${user%:*}
# Avoid useless use of cat
# Anchor regex correctly
if ! grep -q "^$user:" /etc/passwd
then
# Quote user
sudo useradd "$user"
else
setfacl -m "$acl" "$file"
fi
done <"$1"
else
# Error message to stderr
echo Enter the correct path of the configuration file. >&2
# Signal failure to the caller
return 1
fi
}
# Properly quote argument
SetRights "$1"

Varying Vagrant Vagrants provision script issue

I'm trying to write a provision script for Browscap, and I'm almost there, I just want to see what I'm doing wrong when trying to rewrite php.ini file.
The repo is here.
The provision script looks like this
#!/usr/bin/env bash
# Prettyfiers
BLUE='\033[0;36m'
NC='\033[0m' # No Color
DIR=`dirname $0`
echo "${BLUE}Setting up Browsecap${NC}"
# Check PHP version
PHP_VER=`php -r \#phpinfo\(\)\; | grep 'PHP Version' -m 1 | grep -Po -m 1 '(\d+\.\d+)' | head -1`
MIN_REQ="5.3"
if (( $(echo "${PHP_VER} < ${MIN_REQ}" |bc -l) )); then
echo "${BLUE}The PHP version is lower than 5.3 so browscap won't work. Please upgrade your PHP version to higher than 5.3${NC}"
exit 0
fi
echo "${BLUE}PHP version is${NC}" ${PHP_VER}
BROWSE_INI="/etc/php/${PHP_VER}/mods-available/php_browscap.ini"
# Check if browscap is already installed/set up
if [[ -f "${BROWSE_INI}" ]]; then
echo "${BLUE}Browscap is already installed${NC}"
else
# Set the browscap.ini and the php extension
touch "${BROWSE_INI}"
cp "php_browscap.ini" "${BROWSE_INI}"
echo "${BLUE}Browscap copied${NC}"
fi
PHP_INI="/etc/php/${PHP_VER}/fpm/php.ini"
# Check if php.ini exists before replacing
if [[ -f "${PHP_INI}" ]]; then
echo "${BLUE}php.ini exists${NC}"
# Check if the default value exists - by default it should be ;browscap = extra/browscap.ini
# If it doesn't then find browscap = and replace it with the correct one
if [ "$(grep -qe ";browscap" "${PHP_INI}")" ]; then
sudo sed -i "s|;browscap =|browscap = /etc/php/${PHP_VER}/mods-available/php_browscap.ini|g" "/etc/php/${PHP_VER}/fpm/php.ini"
else
sudo sed -i "s|browscap =|browscap = /etc/php/${PHP_VER}/mods-available/php_browscap.ini|g" "/etc/php/${PHP_VER}/fpm/php.ini"
fi
echo "${BLUE}php.ini changed${NC}"
else
echo "${BLUE}php.ini doesn't exist${NC}"
fi
When the provision ends and I check my php.ini I get
[browscap]
; http://php.net/browscap
;browscap = /etc/php/7.0/mods-available/php_browscap.ini /etc/php/7.0/mods-available/php_browscap.ini extra/browscap.ini
So I'm missing something in my sed command.
But what?
MVE
If you have VVV installed you can go to shh with vagrant ssh
Then go to /home/vagrant and create text.txt and test.sh
text.txt
[browscap]
;browscap = extra/browscap.ini
test.sh
#!/usr/bin/env bash
TEST_FILE="/home/vagrant/text.txt"
# Check if php.ini exists before replacing
if [[ -f "${TEST_FILE}" ]]; then
echo "text.txt exists"
# Check if the default value exists - by default it should be ;browscap = extra/browscap.ini
# If it doesn't then find browscap = and replace it with the correct one
if grep -q ";browscap" "${TEST_FILE}"; then
sudo sed -i "s|;browscap =|browscap = /etc/php/7.0/mods-available/php_browscap.ini|g" "${TEST_FILE}"
else
sudo sed -i "s|browscap =|browscap = /etc/php/7.0/mods-available/php_browscap.ini|g" "${TEST_FILE}"
fi
echo "text.txt changed"
else
echo "text.txt doesn't exist"
fi
This will result in text.txt
[browscap]
browscap = /etc/php/7.0/mods-available/php_browscap.ini extra/browscap.ini
Rewrite this statement
if [ "$(grep -qe ";browscap" "${PHP_INI}")" ]; then
to just
if grep -q ";browscap" "${PHP_INI}"; then
since you can directly use grep's return code in shell conditionals. The reason being in the former case, you are incorrectly checking the return code of grep to see if it succeeded, because the exit code is processed by the shell.
(or) alternatively you could also do
grep -qe ";browscap" "${PHP_INI}"
rc=$?
if [ $rc -eq 0 ]; then
echo 'match found'
fi
because grep returns a different exit code if its found something (zero) vs. if it hasn't found something (non-zero). In an if statement, a zero exit code is mapped to "true" and a non-zero exit code is mapped to false.
Also your sed statement should include the matching part after = which needs to be done as
sed -i "s|;browscap =.*|browscap = /etc/php/${PHP_VER}/mods-available/php_browscap.ini|g" "/etc/php/${PHP_VER}/fpm/php.ini"
Based on the MCVE you posted, you should replace this block of code:
if grep -q ";browscap" "${TEST_FILE}"; then
sudo sed -i "s|;browscap =|browscap = /etc/php/7.0/mods-available/php_browscap.ini|g" "${TEST_FILE}"
else
sudo sed -i "s|browscap =|browscap = /etc/php/7.0/mods-available/php_browscap.ini|g" "${TEST_FILE}"
fi
with just 1 line:
sudo sed -i 's|;?browscap =.*|browscap = /etc/php/7.0/mods-available/php_browscap.ini|' "${TEST_FILE}"

Bash if grep or grep elif grep? So confused

Here is what I am trying to do. I want to grep(maybe?) a file for keywords based on what I find do blah.
if grep "env=" does not exist OR "env=" exists but is null file.name; then
env=prod
elif grep "env=dev" OR "env=qa" file.name; then
env=dev/qa respectivly
I was trying some grep command but was getting stuck with that
Not sure if this gets it yet, still need to test
if ! grep -q -E "^environment=" "${__file}" | grep -q -e "dev|qa|prod"; then
if [ -n "${ENV}" ]; then
echo "environment=${ENV}" >> "${__file}"
else
echo "environment=prod" >> "${__file}"
fi
fi
Use a source command. Put all local variables in file named local_env.sh.
sample content of local_env.sh:
environment=dev
Now you can create a script:
source env.sh
# if environment is empty string set it to prod
[ -z "${environment}" ] && environment=prod
echo ${environment}

Bash sub script redirects input to /dev/null mistakenly

I'm working on a script to automate the creation of a .gitconfig file.
This is my main script that calls a function which in turn execute another file.
dotfile.sh
COMMAND_NAME=$1
shift
ARG_NAME=$#
set +a
fail() {
echo "";
printf "\r[${RED}FAIL${RESET}] $1\n";
echo "";
exit 1;
}
set -a
sub_setup() {
info "This may overwrite existing files in your computer. Are you sure? (y/n)";
read -p "" -n 1;
echo "";
if [[ $REPLY =~ ^[Yy]$ ]]; then
for ARG in $ARG_NAME; do
local SCRIPT="~/dotfiles/setup/${ARG}.sh";
[ -f "$SCRIPT" ] && echo "Applying '$ARG'" && . "$SCRIPT" || fail "Unable to find script '$ARG'";
done;
fi;
}
case $COMMAND_NAME in
"" | "-h" | "--help")
sub_help;
;;
*)
CMD=${COMMAND_NAME/*-/}
sub_${CMD} $ARG_NAME 2> /dev/null;
if [ $? = 127 ]; then
fail "'$CMD' is not a known command or has errors.";
fi;
;;
esac;
git.sh
git_config() {
if [ ! -f "~/dotfiles/git/gitconfig_template" ]; then
fail "No gitconfig_template file found in ~/dotfiles/git/";
elif [ -f "~/dotfiles/.gitconfig" ]; then
fail ".gitconfig already exists. Delete the file and retry.";
else
echo "Setting up .gitconfig";
GIT_CREDENTIAL="cache"
[ "$(uname -s)" == "Darwin" ] && GIT_CREDENTIAL="osxkeychain";
user " - What is your GitHub author name?";
read -e GIT_AUTHORNAME;
user " - What is your GitHub author email?";
read -e GIT_AUTHOREMAIL;
user " - What is your GitHub username?";
read -e GIT_USERNAME;
if sed -e "s/AUTHORNAME/$GIT_AUTHORNAME/g" \
-e "s/AUTHOREMAIL/$GIT_AUTHOREMAIL/g" \
-e "s/USERNAME/$GIT_USERNAME/g" \
-e "s/GIT_CREDENTIAL_HELPER/$GIT_CREDENTIAL/g" \
"~/dotfiles/git/gitconfig_template" > "~/dotfiles/.gitconfig"; then
success ".gitconfig has been setup";
else
fail ".gitconfig has not been setup";
fi;
fi;
}
git_config
In the console
$ ./dotfile.sh --setup git
[ ?? ] This may overwrite existing files in your computer. Are you sure? (y/n)
y
Applying 'git'
Setting up .gitconfig
[ .. ] - What is your GitHub author name?
Then I cannot see what I'm typing...
At the bottom of dotfile.sh, I redirect any error that occurs during my function call to /dev/null. But I should normally see what I'm typing. If I remove 2> /dev/null from this line sub_${CMD} $ARG_NAME 2> /dev/null;, it works!! But I don't understand why.
I need this line to prevent my script to echo an error in case my command doesn't exists. I only want my own message.
e.g.
$ ./dotfile --blahblah
./dotfiles: line 153: sub_blahblah: command not found
[FAIL] 'blahblah' is not a known command or has errors
I really don't understand why the input in my sub script is redirected to /dev/null as I mentioned only stderr to be redirected to /dev/null.
Thanks
Do you need the -e option in your read statements?
I did a quick test in an interactive shell. The following command does not echo characters :
read -e TEST 2>/dev/null
The following does echo the characters
read TEST 2>/dev/null

Check if file exists [BASH]

How do I check if file exists in bash?
When I try to do it like this:
FILE1="${#:$OPTIND:1}"
if [ ! -e "$FILE1" ]
then
echo "requested file doesn't exist" >&2
exit 1
elif
<more code follows>
I always get following output:
requested file doesn't exist
The program is used like this:
script.sh [-g] [-p] [-r FUNCTION_ID|-d FUNCTION_ID] FILE
Any ideas please?
I will be glad for any help.
P.S. I wish I could show the entire file without the risk of being fired from school for having a duplicate. If there is a private method of communication I will happily oblige.
My mistake. Fas forcing a binary file into a wrong place. Thanks for everyone's help.
Little trick to debugging problems like this. Add these lines to the top of your script:
export PS4="\$LINENO: "
set -xv
The set -xv will print out each line before it is executed, and then the line once the shell interpolates variables, etc. The $PS4 is the prompt used by set -xv. This will print the line number of the shell script as it executes. You'll be able to follow what is going on and where you may have problems.
Here's an example of a test script:
#! /bin/bash
export PS4="\$LINENO: "
set -xv
FILE1="${#:$OPTIND:1}" # Line 6
if [ ! -e "$FILE1" ] # Line 7
then
echo "requested file doesn't exist" >&2
exit 1
else
echo "Found File $FILE1" # Line 12
fi
And here's what I get when I run it:
$ ./test.sh .profile
FILE1="${#:$OPTIND:1}"
6: FILE1=.profile
if [ ! -e "$FILE1" ]
then
echo "requested file doesn't exist" >&2
exit 1
else
echo "Found File $FILE1"
fi
7: [ ! -e .profile ]
12: echo 'Found File .profile'
Found File .profile
Here, I can see that I set $FILE1 to .profile, and that my script understood that ${#:$OPTIND:1}. The best thing about this is that it works on all shells down to the original Bourne shell. That means if you aren't running Bash as you think you might be, you'll see where your script is failing, and maybe fix the issue.
I suspect you might not be running your script in Bash. Did you put #! /bin/bash on the top?
script.sh [-g] [-p] [-r FUNCTION_ID|-d FUNCTION_ID] FILE
You may want to use getopts to parse your parameters:
#! /bin/bash
USAGE=" Usage:
script.sh [-g] [-p] [-r FUNCTION_ID|-d FUNCTION_ID] FILE
"
while getopts gpr:d: option
do
case $option in
g) g_opt=1;;
p) p_opt=1;;
r) rfunction_id="$OPTARG";;
d) dfunction_id="$OPTARG";;
[?])
echo "Invalid Usage" 1>&2
echo "$USAGE" 1>&2
exit 2
;;
esac
done
if [[ -n $rfunction_id && -n $dfunction_id ]]
then
echo "Invalid Usage: You can't specify both -r and -d" 1>&2
echo "$USAGE" >2&
exit 2
fi
shift $(($OPTIND - 1))
[[ -n $g_opt ]] && echo "-g was set"
[[ -n $p_opt ]] && echo "-p was set"
[[ -n $rfunction_id ]] && echo "-r was set to $rfunction_id"
[[ -n $dfunction_id ]] && echo "-d was set to $dfunction_id"
[[ -n $1 ]] && echo "File is $1"
To (recap) and add to #DavidW.'s excellent answer:
Check the shebang line (first line) of your script to ensure that it's executed by bash: is it #!/bin/bash or #!/usr/bin/env bash?
Inspect your script file for hidden control characters (such as \r) that can result in unexpected behavior; run cat -v scriptFile | fgrep ^ - it should produce NO output; if the file does contain \r chars., they would show as ^M.
To remove the \r instances (more accurately, to convert Windows-style \r\n newline sequences to Unix \n-only sequences), you can use dos2unix file to convert in place; if you don't have this utility, you can use sed 's/'$'\r''$//' file > outfile (CAVEAT: use a DIFFERENT output file, otherwise you'll destroy your input file); to remove all \r instances (even if not followed by \n), use tr -d '\r' < file > outfile (CAVEAT: use a DIFFERENT output file, otherwise you'll destroy your input file).
In addition to #DavidW.'s great debugging technique, you can add the following to visually inspect all arguments passed to your script:
i=0; for a; do echo "\$$((i+=1))=[$a]"; done
(The purpose of enclosing the value in [...] (for example), is to see the exact boundaries of the values.)
This will yield something like:
$1=[-g]
$2=[input.txt]
...
Note, though, that nothing at all is printed if no arguments were passed.
Try to print FILE1 to see if it has the value you want, if it is not the problem, here is a simple script (site below):
#!/bin/bash
file="${#:$OPTIND:1}"
if [ -f "$file" ]
then
echo "$file found."
else
echo "$file not found."
fi
http://www.cyberciti.biz/faq/unix-linux-test-existence-of-file-in-bash/
Instead of plucking an item out of "$#" in a tricky way, why don't you shift off the args you've processed with getopts:
while getopts ...
done
shift $(( OPTIND - 1 ))
FILE1=$1

Resources