I am trying to make a bash script for each time I set up a new linux system. My problem is I am getting an error saying parse error:
condition expected: =
I think the problem is that it is inside a while loop because it works when I remove it, but the while loop is kinda important for my program so cant just remove it. The script is meant to make a list which shows the options I can do in my script and it is supposed to have colors and a way to check if the option has ben ran before. The script seems to stop working after the line:
while [ $loop = 1 ]; do
When I try to run without the while loop it works so I think this may be a cause of the problem.
loop=1
scriptstatus=(0 0 0 0 0 0 0 0 );
whatitdoes=('Install terminal programs' 'Install security programs' 'Install normal programs' 'Install spotify' 'Customize gnome' 'Install and customize zsh' 'Install etcher' 'Not yet run' 'Not yet run');
#'$(tput bold)$(tput setaf 1)Not yet run$(tput sgr0)'
while [ $loop = 1 ]; do
answer=""
clear
echo "$(tput bold) Script: What script does: Status:$(tput sgr0)"
COUNTER=0
while [ $COUNTER -lt 7 ]; do
if [[ $scriptstatus[$COUNTER] = 0 ]]
then
echo " ["$COUNTER"] "$'\x1d'" ${whatitdoes[$COUNTER]} "$'\x1d'" $(tput bold)$(tput setaf 1)Not yet run$(tput sgr0)"
elif [[ $scriptstatus[$COUNTER] = 1 ]]
then
echo " ["$COUNTER"] "$'\x1d'" ${whatitdoes[$COUNTER]} "$'\x1d'" $(tput bold)$(tput setaf 2)Completed$(tput sgr0)"
else
echo "error"
fi
let COUNTER=COUNTER+1
done | column -t -s$'\x1d'
echo "quit: Exits the program"
echo "Choose one option:"
read answer
done
Turns out I just wrote the array expansion wrongly; instead of:
if [[ $scriptstatus[$COUNTER] = 0 ]]
I had to use:
if [[ ${scriptstatus[$COUNTER]} = 0 ]]
The { and } are required for array subscripting.
Related
So I am learning bash and wanted to make a script to 'automate' a collection of kali tools. Basically I do not understand why the if statement code is being triggered regardless if condition is true or false.
#!/bin/bash
#requires root or sudo priv
#check for a successful ping
target_ip=$1
nmap_opt = " "
#nmap options variables --
nmap_os="-O"
nmap_version="-sV"
nmap_udp="-sU"
nmap_stealth="-sS"
nmap_aggr="-A"
nmap_option_input_temp=""
ping $target_ip -c 5
if [ $? -eq 0 ]; then
echo "successful ping on" $target_ip
else
echo "ping unsuccessful, check VPN connection or host may be down"
exit 1
fi
read -p "enter pathway for nmap output: " nmap_file
read -p "detect os? (y/n) " nmap_option_input_temp
#add loops for y/n user input
if [[ ${nmap_option_input_temp} -eq "y" ]];
then
nmap_opt="${nmap_opt} ${nmap_os}"
elif [[ ${nmap_option_input_temp} -eq "n" ]];
then
nmap_opt=$nmap_opt
else
echo "invalid option"
fi
read -p "detect version? (y/n) " nmap_option_input_temp
#add loops for y/n user input
if [[ ${nmap_option_input_temp} -eq "y" ]];
then
nmap_opt="${nmap_opt} ${nmap_version}"
elif [[ ${nmap_option_input_temp} -eq "n" ]];
then
nmap_opt=$nmap_opt
else
echo "invalid option"
fi
echo "nmap selected option/s:" $nmap_opt
echo $nmap_option_input_temp
#starting nmap..
sudo nmap $nmap_opt -oN $nmap_file $target_ip
this is the nmap_opt variable echo out even when both inputs are 'n'
nmap selected option/s: -O -sV
Let me know if there is anything I missed in the explanation!
-eq is for integer comparisons. Use ==:
if [[ "${nmap_option_input_temp}" == y ]];
And fix your quotes. Fixed strings like y don't need to be quoted. Inside [[, variables don't need to be quoted, but IMO you should quote them anyway for consistency.
This error would be more easily spotted if you used the more conventional:
if [ "${nmap_option_input_temp}" -eq y ]; # Still incorrect!!
since you would get a nice error message.
Note that the fix with [ is slightly different, and you should use a single =:
if [ "${nmap_option_input_temp}" = y ];
The == operator is a bashism which does not really add any value. But really, you should not be using if/elif at all here. This is the perfect place for a case statement:
case "${nmap_option_input_temp}" in
y) nmap_opt="${nmap_opt} ${nmap_os}";;
n) ;;
*) echo "invalid option" >&2 ;;
esac
The parallel execution is working properly as expected. But i need to get the states of each file. Here I need to check it by using the if condition. This is just a sample and i have many scenarios like this in my code.
I'm a beginner at #help shell scripts & linux but actually i love linux now.
I'm expecting an alternative method or a help to resolve this. Thank you everyone.
( echo $FILE_PATH/import_csv_location_data_into_hive.sh; echo $FILE_PATH/import_csv_order_data_into_hive.sh; echo $FILE_PATH/import_csv_order_with_delivery_info_into_hive.sh; ) | parallel bash
wait
IMPORTED_LOCATIONS_STATUS=$? IMPORTED_ORDERS_STATUS=$? IMPORTED_ORDERS_WITH_DELIVERY_INFO_STATUS=$?
if [[ "$IMPORTED_LOCATIONS_STATUS" == "0" && "$IMPORTED_ORDERS_STATUS" == "0" && "$IMPORTED_ORDERS_WITH_DELIVERY_INFO_STATUS" == "0" ]];
then
echo "COMPLETED."
exit 0
else
STATUS="FAILED."
exit 1
fi
Sample code
enter image description here
(echo Your-Special-Header; \
echo whoami; \
echo cat nothere \
) | parallel --header : --results all.csv bash -c
The file nothere does not exist. Here is the csv output:
Seq,Host,Starttime,JobRuntime,Send,Receive,Exitval,Signal,Command,Your-Special-Header,Stdout,Stderr
1,:,1662823208.481,0.001,0,5,0,0,"bash -c whoami",whoami,"root
",
2,:,1662823208.481,0.003,0,0,1,0,"bash -c cat\ nothere","cat nothere",,"cat: nothere: No such file or directory
"
The 7th column is Exitval.
If you only need to know if all finished with no errors:
(
echo $FILE_PATH/import_csv_location_data_into_hive.sh;
echo $FILE_PATH/import_csv_order_data_into_hive.sh;
echo $FILE_PATH/import_csv_order_with_delivery_info_into_hive.sh;
) | parallel bash
if [[ "$?" == "0" ]]; then
echo "COMPLETED."
exit 0
else
STATUS="FAILED."
exit 1
fi
Shorter, but less readable IMO:
(
echo $FILE_PATH/import_csv_location_data_into_hive.sh;
echo $FILE_PATH/import_csv_order_data_into_hive.sh;
echo $FILE_PATH/import_csv_order_with_delivery_info_into_hive.sh;
) | if parallel bash ; then
echo "COMPLETED."
exit 0
else
STATUS="FAILED."
exit 1
fi
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
using OR in shell script
(2 answers)
Closed 7 months ago.
here is my simple script prova.sh:
#!/bin/bash
echo "\$# = [$#]"
echo "\$1 = [$1]"
if [ $# -ge 2 || $1="-h" ]
then
echo "#########################################################################"
echo -e "usage: $0 \t launches the program only"
echo -e " $0 -s\t shows the command fired and launches the program"
echo -e " $0 -so\t shows only the command fired"
echo -e " $0 -h\t shows this guide"
echo "#########################################################################"
exit 0
fi
command="ls -la"
if test "$1"="-so" || "$1"="-s"
then
echo "Fired command:"
echo $command
fi
if test "$1"!="-so"
then
$command
fi
here is the output:
$> ./prova.sh -h
$# = [1]
$1 = [-h]
./prova.sh: row 6 : [: "]" missing
./prova.sh: row6: -h=-h: command not found
Fired command:
ls -l
totale 4
-rwxrw-r--. 1 user user 632 20 lug 16.13 prova.sh
I expected just the help
you need to close every test using single brackets, the shell way, if [ $# -ge 2 ] || [ $1 = "-h" ]; then ... this will fix it. If you still use single brackets way, is safe to enclose you variables with double quotes, so if they are empty, you are safe, like this if [ "$#" -ge 2 ] || [ "$1" = "-h" ]; then ... or you can just put them inside a double bracket, and bash will handle this for you if [[ $# -ge 2 || $1 = "-h" ]]; then ...
I have a pretty simple bash script that coordinates running a couple python scripts. What I am having trouble figuring out is why after running the bash script (. bash_script.sh), the terminal hangs. I can't ctrl+c, ctrl+z or do anything except restart the SSH session. All I see is just a blinking cursor. Checking all the log files indicates a 0 status exit code with no errors in the scripts themselves. Running ps aux | grep bash_script.sh does not show any anything running either. Is there anyway to debug this?
#!/bin/bash
exec >> <DIR>/logfile.log 2>&1
script_message () {
status_arg=$1
if [[ $status_arg = "pass" ]]; then
printf "Script Done\n"
printf '=%.0s' {1..50}
printf "\n"
elif [[ $status_arg = "fail" ]]; then
printf "Script Failed\n"
printf '=%.0s' {1..50}
printf "\n"
else
:
fi
}
current_date=$(date '+%Y-%m-%d %H:%M:%S')
day=$(date +%u)
hour=$(date +%H)
printf "RUN DATE: $current_date\n"
# activate virtual env
source /<VENV DIR/bin/activate>
python <PYTHON SCRIPT>.py >> <DIR>/logfile2.log 2>&1
retVal=$?
if [[ $retVal -eq 0 && $day -eq 4 ]]; then
python <PYTHON SCRIPT 2>.py >> <DIR>/logfile3.log 2>&1
script_message pass
elif [[ $retVal -eq 0 ]]; then
script_message pass
else
#:
script_message fail
fi
echo $?
i have a question about simple shell script.
this is the source code of rand.sh below
#!/bin/bash
n=$(( RANDOM % 100 ))
if [[ n -eq 42 ]]; then
echo "Something went wrong"
>&2 echo "The error was using magic numbers"
exit 1
fi
echo "Everything went accrding to plan"
and i'm going to make a new shell script, let me call it quiz.sh.
quiz.sh should loop until n==42. if n==42, save the stdout("Something went wrong") and stderr("The error was using magic numbers")
and it finally terminated with printing out those stdout,stderr and Total execution count.
here is my quiz.sh
#!/bin/bash
cnt=0
while [[ "${n}" -ne 42 ]]
do
(( cnt = "${cnt}"+1 ))
source ./rand.sh &> error.txt
done
cat error.txt
echo "${cnt}"
but this is not working. because of exit 1 in rand.sh, the program is terminated before executing cat and echo which is at the end two line.
how can i fix it?? please let me know!
I want to make happen cat error.txt and echo "${cnt}" as well
Run the loop in a subshell
(
while something; do
something
exit 1 # exits only from the subshell
done
)
Note: parent shell doesn't access/inherit child process environment. So cnt is going to be empty in parent shell. Transfer it some other way.
(
cnt=0
while ((n != 42)); do
((cnt++))
echo "$cnt" > cntfile.txt
# >& is deprecated
source myrand > error.txt 2>&1
done
)
cnt=$(<cntfile.txt)
cat error.txt
echo "$cnt"
Reference Bash manual command grouping.
As KamilCuk pointed out correctly, you should use $n instead of n.
Furthermore, I personally would add that using source ./rand.sh &> error.txt is kind of weird in this case. If you want to run it as a background process, use:
./rand.sh &> error.txt &
wait $! # $! is the pid
Otherwise, just make a function out of it:
#!/bin/bash
function myrand {
n=$(( RANDOM % 100 ))
if [[ n -eq 42 ]]; then
echo "Something went wrong"
>&2 echo "The error was using magic numbers"
return 1
fi
echo "Everything went accrding to plan"
return 0
}
cnt=0
while [[ "${n}" -ne 42 ]]
do
(( cnt = "${cnt}"+1 ))
myrand() &> error.txt
done
cat error.txt
echo "${cnt}"
p.s. code not tested, but I guess it works.