SO I wrote a script that can change the SSH port on CentOS but for some reason I'm encountering this error:
sshchangecOS6.sh: line 36: syntax error: unexpected end of file
This is the script:
#! /bin/bash
# This script changes the ssh port for logins on CentOS 5 and 6
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 2
read -r -p "Would you like to change the ssh port? [Y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
read -p "What would you like to change the port to? (Chose between 1024-65535) " sshportconfig
if (( ("$sshportconfig" > 1024) && ("$sshportconfig" < 65535) )); then
echo "Port $sshportconfig" >> /etc/ssh/sshd_config
echo "--------------------------------------------------------------------"
echo ""
echo ""
echo "SSH port has been changed to: $sshportconfig. Written by Sincere the Minotaur."
echo ""
echo ""
echo "--------------------------------------------------------------------"
else
echo "Port chosen is incorrect."
exit 1
fi
else
sshPort=$(grep "Port" /etc/ssh/sshd_config) | head -n 1
echo "--------------------------------------------------------------------"
echo ""
echo ""
echo "SSH is still: $sshPort"
echo "Written by Sincere the Minotaur."
echo ""
echo "---------------------------------------------------------------------"
exit 1
fi
exit 0
Could someone explain where the errors are?
There is no fi for the first if if [[ $EUID -ne 0 ]]; then. You need to close every if with a fi.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
im trying to create a simple bash script to create and delete a user on ubunutu , here is my script
sudo nano createuser.sh
#!/bin/bash
choice=2
# Main Display
echo "Enter Number to select an option"
echo
echo "1) Add User"
echo "2) Delete User"
echo
while [ $choice -eq 2 ]; do
read choice
if [ $choice -eq 1] ;
then
echo -e "Enter Username"
read user_name
echo -e "Enter Password"
read user_passwd
sudo useradd $user_name -m -p $user_passwd
cat /etc/passwd
else if [$choise -eq 2] ; then
cat /etc/passwd
echo -e "Enter Password"
read del_passwd
echo -e "User to be deleted:"
read del_user
sudo userdel -r $del_user
cat /etc/passwd
echo
fi
im not sure if there s a typo on my script ,or something else .
whenever i execute the script i get this message
Enter Number to select an option
Add User
Delete User
./createuser.sh: line 31: syntax error: unexpected end of file
thank you in advance for your help !!
Errors:
wrong if/else/fi sequence, what you have is basically this w few errors
if [ ]
then
# something
else
if [ ]
then
# something else
fi
# fi should be here ti close outer if []
In bash you have if then/elif/else closed by fi So something like this
if []
then
# something
elif []
then
# something else happened
else
# something else than elif happened
fi
; after if [], it only goes there if if and than are in the same line, like so
if [] ; then
# something
elif []
# something else happened
else
# something else than elif happened
fi
space inside test brackets []
if [ a -eq 5 ]
# ^ ^
# +-------+----< notice space here
In bash while sequence goes as following while [ ] do done. like following
while [ i -le 55 ]
do
# do something
done
Suggestions
use -s for reading in password in bash to hide it while typing.
Conclusion, with all the fixes above here is working script:
#!/bin/bash
choice=2
# Main Display
echo "Enter Number to select an option"
echo
echo "1) Add User"
echo "2) Delete User"
echo
while [ $choice -eq 2 ]
do
read choice
if [ $choice -eq 1 ]
then
echo -e "Enter Username"
read user_name
echo -e "Enter Password"
read user_passwd
sudo useradd $user_name -m -p $user_passwd
cat /etc/passwd
elif [ $choise -eq 2 ]
then
cat /etc/passwd
echo -e "Enter Password"
read del_passwd
echo -e "User to be deleted:"
read del_user
sudo userdel -r $del_user
cat /etc/passwd
echo
else
echo "Wrong option you have 1 or 2"
fi
done
yes guys ,thank you so much for your help
i just fixed it
here is my working script now
#!/bin/bash
choice=2
# Main display echo "Enter number to select an option" echo echo "1) Add User" echo "2) Delete User" echo
while [[ "$choice" -eq 2 ]]; do
read choice
if [[ "$choice" -eq 1 ]] ; then
echo -e "Enter Username"
read user_name
echo -e "Enter Password"
read user_passwd
sudo useradd "$user_name" -m -p "$user_passwd"
cat /etc/passwd else
if [[ "$choice" -eq 2 ]] ; then
cat /etc/passwd
echo
echo -e "User to be deleted:"
read del_user
sudo userdel -r "$del_user"
cat /etc/passwd
echo
choice=2
fi
fi
done
I made this bash script but getting this error when running it: ./admin2.sh: line 78: syntax error near unexpected token else'
./admin2.sh: line 78:else'.
I've edited it many times but i cant seem to find what exactly the error is. this is the script:
#!/bin/bash
if [[ $key == 1029127 ]]
clear
echo -e ""
echo -e -n "${LIGHTRED}[!] ${WHITE}Loading admin menu"
spinner () {
local SP_WIDTH="$3"
local SP_DELAY="$4"
local SP_STRING=${2:-"'|/=\'"}
local SP_COLOR=0
tput civis
while [ -d /proc/$1 ]; do
((RANDOM%2 == 0)) && SP_COLOR=3$((RANDOM%8)) ||
SP_COLOR=9$((RANDOM%8))
printf "\e[1;${SP_COLOR}m\e7 %${SP_WIDTH}s \e8\e[0m" "$SP_STRING"
sleep ${SP_DELAY:-.2}
SP_STRING=${SP_STRING#"${SP_STRING%?}"}${SP_STRING%?}
done
tput cnorm
}
sleep 2.5 &
spinner "$!" '-\\|/' '1.1' '.2'
tput civis
sleep 1
tput cnorm
while true
do
clear
echo -e "${LIGHTCYAN} Welcome"
echo -e ""
echo -e -n "${WHITE}- Current IP:${LIGHTRED} "
w|awk '{if(NR>2){print $3}}' $3
echo -e -n "${WHITE}- Users connected:${LIGHTRED} "
users | wc -w
echo -e "${WHITE}- Admin privileges:${WHITE
[${LIGHTGREEN}Enabled${WHITE}]"
echo -e ""
echo -e "${LIGHTRED} //Announcements//"
echo -e ""
echo -e "${YELLOW}- Type: /help to see commands"
echo -e "\n"
echo -e ""
echo -e ""
echo -e -n "${LIGHTRED}Type: \c"
read answer
else
echo -e ""
echo -e "${LIGHTRED}[!] ${WHITE}Incorrect key, access denied.
fi
You also seem to have forgotten to end the second while loop. You should end it by adding a doneon the line before the else
...
read answer
done
else
echo -e ""
echo -e "${LIGHTRED}[!] ${WHITE}Incorrect key, access denied.
fi
You're missing a then after your if statement on line 2:
if [[ $key == 1029127 ]]
then
...
else
...
fi
Many people prefer to put the then on the same line, as:
if [[ $key == 1029127 ]]; then
...
else
...
fi
Either way I'd encourage you to properly indent your code so that it's easier to read, and be consistent about style choices such as putting then and do on separate lines or not.
Hello I am new to shell script and I need to handle the error coming from a command being excuted inside echo like the following
echo -e "some internal command that I can't share \nq" | nc localhost 10000
I want to say
if [[ there's no error ]]
try
echo "YOUR SUPERSECRET COMMAND" | nc localhost 10000 | grep "Your expected error"
if [[ $? -eq 0 ]]; then
echo "Do something useful with error"
else
echo "Success"
fi
grep return 0 on matching and returns 1 when it doesn't find matching string.
The shell variable $? will give you the exit code. So, you could do:
echo -e "some internal command that I can't share \nq" | nc localhost 10000
rc=$?
if [[ $rc == 0 ]]; then
echo "success"
fi
Or simply,
if echo -e "some internal command that I can't share \nq" | nc localhost 10000; then
echo "success"
fi
Here is a concise way of doing it:
internalcommand &>/dev/null 2>&1 && echo OK || echo FAILED
If internalcommand succeeds OK will be printed to stdout, if it fails FAILED is printed.
Note tested on Bash v4
I have a very peculiar issue with a script that I have wrote today. I am trying to form an ip address from two variables namely url and port. I am getting the url value from a library script which echos 10.241.1.8 and the port number is 10000. Now if I concatenate both the url and the port into another variable ip, I get completely a strange result(:10000241.1.8). I have my code and its result below. Please help me with your suggestions to fix this.
clear
echo $(date +'%H:%M:%S')'>> "Sample Records" Script started...'
usage() {
echo ">> $ script.sh -ctoff 89 -env c -ns reporting -depPath /user/release/audit_prime_oozie"
echo "Usage: $ script.sh -ctoff <Cutoff number> -env <testing cluster. ex: s for staging,c,d,p and a> -ns <optional: hive namespace> -depPath <deployment path>"
}
# Function to validate if value of a parameter is not empty
validate () {
if [[ $flag != 1 ]]; then
if [[ $tmpVar == *"-"* ]] || [[ -z $tmpVar ]]; then
usage
exit 1
fi
fi
}
options=$#
if [[ -z $options ]]; then
usage
exit 1
fi
arguments=($options)
index=0
# Function to extract the parameter values
check (){
for x in $options
do
index=`expr $index + 1`
case $x in
-ctoff)
cutOff="${arguments[index]}"
tmpVar=$cutOff
validate $tmpVar
;;
-env)
env="${arguments[index]}"
tmpVar=$env
validate $tmpVar
;;
-ns)
ns="${arguments[index]}"
tmpVar=$ns
validate $tmpVar
;;
-depPath)
depPath="${arguments[index]}"
tmpVar=$depPath
validate $tmpVar
;;
esac
if [[ -z $ns ]];then
ns=reporting
fi
done
}
check $#
error_exit(){
echo "$1" 1>&2
exit 1
}
# Create the execution directory
user=$(id -u -n)
PWD=`pwd`
INSTALL_ROOT=$PWD
LOCAL_DIR="/tmp/$user/sample_duns"
if [[ ! -d $LOCAL_DIR ]]; then
mkdir -p $LOCAL_DIR
echo ">> Created local directory $LOCAL_DIR"
if [[ $? -ne 0 ]]; then
echo ">> Unable to create $LOCAL_DIR, writing to current folder $INSTALL_ROOT"
LOCAL_DIR=$INSTALL_ROOT
fi
fi
if [[ $(ls -A $LOCAL_DIR) ]]; then
echo ">> Removed the temp files from $LOCAL_DIR"
rm -r $LOCAL_DIR/*
fi
# create the file name
datestamp=$(date '+%Y%m%d%H')
outFile=sample_duns_$datestamp.txt
# Copy the contents from HDFS to Local directory
echo ">> Copying required files from HDFS"
hdfs dfs -copyToLocal $depPath/data-warehouse/config/server.properties $LOCAL_DIR || error_exit "Cannot copy files from HDFS! Exiting now.."
hdfs dfs -copyToLocal $depPath/data-warehouse/reporting/lib_getHiveServer2ip.sh $LOCAL_DIR || error_exit "Cannot copy files from HDFS! Exiting now.."
if [[ $? -ne 0 ]]; then
echo ">> Files missing. Exiting now.."
exit 1
fi
# Call the lib script to get appropriate hiveserver2 ip address from the supplied environment for beeline execution
echo ">> Reading the HiveServer2 ip"
chmod +x $LOCAL_DIR/lib_getHiveServer2ip.sh
url=$($LOCAL_DIR/lib_getHiveServer2ip.sh $env $LOCAL_DIR/server.properties)
echo url=$url
port=10000
echo ip=$url:$b
Here is my output from the terminal.
11:18:16>> "Sample Records" Script started...
>> Removed the temp files from /tmp/user/sample_duns
>> Copying required files from HDFS
>> Reading the HiveServer2 ip
url=10.241.1.8
:10000241.1.8
I am expecting the below result
ip=10.241.1.8:10000
Adding the lib_getHiveServer2ip.sh script below
. $2 # read properties file
if [[ $1 == "d" ]]; then
ip=$devHSer
elif [[ $1 == "c" ]]; then
ip=$crankHSer
elif [[ $1 == "s" ]]; then
ip=$stgHSer
elif [[ $1 == "p" ]]; then
ip=$prdHSer
elif [[ $1 == "a" ]]; then
ip=$alpHSer
else
echo ">> Invalid cluster ip encountered. Exiting now ..."
exit 1
fi
echo $ip
Your url variable contains a carriage return character for some reason. Check lib_getHiveServer2ip.sh for weirdness.
Pipe your echo output to hexdump to confirm.
Edit: looks like your properties file has bad line endings. Use the file utility to check.
I'm trying to create a simple script for logging into various servers via ssh, all keys have been installed and are working but i simply cannot get this script to work for me. Basically there is an option as to which server the user wishes to login to but it keeps throwing up the following error:
': not a valid identifier `INPUT
login.sh: line 24: syntax error near unexpected token `elif'
'ogin.sh: line 24: `elif [ $INPUT -eq 2 ] ; then
The script layout can be found below with dummy info:
#!/bin/bash
echo "What Server would you like to login to?"
echo ""
echo ""
echo "1. Server 1"
echo "2. Server 2"
echo "3. Server 3"
echo "4. Server 4"
echo "5. Exit"
read INPUT
if [ $INPUT -eq 1 ] ; then
echo"Logging in"
echo"..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 2 ] ; then
echo"Logging in"
echo"..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 3 ] ; then
echo"Logging in"
echo"..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 4 ] ; then
echo"Logging in"
echo"..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 5 ] ; then
exit 0
else
echo "invalid choice"
return
fi
Any help would be greatly appreciated, relatively new to using bash and this is just annoying me now!
looks like you created this file on windows.
try, using dos2unix like:
dos2unix <your_script>
This answer is really just a comment, but comments are not suitable for code. You're script could be greatly simplified. Consider something like:
#!/bin/bash
servers=( host1 host2 host3 )
ips=( 192.168.1.1 192.168.1.2 192.168.1.3 )
ports=( 123 22 33 )
select server in ${servers[#]}; do
echo "Logging into $server..."
ssh root#${ips[$REPLY]} -p ${ports[$REPLY]}
break
done
(Although it's not at all clear why you would want to specify the IP addresses rather than using the hostname!)
Tried your script by copy pasting, it worked. I modified a little more on it to get the invalid choice option to work.
Errm... sorry, but it works for me?
#!/bin/bash
while true ; do
echo "What Server would you like to login to?"
echo ""
echo ""
echo "1. Server 1"
echo "2. Server 2"
echo "3. Server 3"
echo "4. Server 4"
echo "5. Exit"
read INPUT
if [ $INPUT -eq 1 ] ; then
echo "Logging in 1"
echo "..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 2 ] ; then
echo "Logging in 2"
echo "..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 3 ] ; then
echo "Logging in 3"
echo "..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 4 ] ; then
echo "Logging in 4"
echo "..."
ssh root#1.2.3.4 -p 5678
elif [ $INPUT -eq 5 ] ; then
exit 0
else
echo "invalid choice"
fi
done
I'll throw myself in front of the SO bus by answering the question you didn't ask on this one. Why not use select?
echo 'Which server would you like to log into?'
select server in 192.168.0.1 192.168.0.2 192.168.0.3 192.168.0.4; do
printf 'Logging in to server %s...\n' "$server"
ssh "$server" -p "$port"
done
If you don't like select, then why not at least use case?
read -p 'Which server do you want? ' input
case "$input" in
whatever)
printf 'Logging in to server %s...\n' "$server"
ssh "$server" -p "$port"
;;
*)
echo 'Invalid option'
;;
esac
I realize this doesn't answer your question, but you said you were new to bash. I'd suggest using the case statement instead of a mess of ifs and I also added a prompt to your read statement (with the -p option). You might also look into the select statement instead of the case.
#!/bin/bash
echo "What Server would you like to login to?"
echo ""
echo ""
echo "1. Server 1"
echo "2. Server 2"
echo "3. Server 3"
echo "4. Server 4"
echo "5. Exit"
read -p "cmd> " INPUT
case $INPUT in
1)
echo "Logging in"
echo "..."
echo ssh root#1.2.3.4 -p 5678
;;
2)
echo "Logging in"
echo "..."
echo ssh root#1.2.3.4 -p 5678
;;
3)
echo "Logging in"
echo "..."
echo ssh root#1.2.3.4 -p 5678
;;
4)
echo "Logging in"
echo "..."
echo ssh root#1.2.3.4 -p 5678
;;
5)
echo "exiting"
exit 0
;;
*)
echo "invalid choice"
return
;;
esac