Shell scripting basic format? [duplicate] - linux

This question already has answers here:
How to if/else statement in shell script
(5 answers)
Closed 3 months ago.
I'm brand new to shell scripting and I've looked up some tutorials about basic shell scripting, but my script still is not running. I'm not totally sure what I'm doing wrong. I'm trying to crack a CTF for some context. The error I'm getting is "line 10: syntax error near unexpected token `done'"
Does anyone have any tips on how to fix this or any resources that could point me in the correct direction?
#!/bin/bash
i=0
while :
do
if [[ "$(md5sum < (echo -n "${i}d470d406"))" =~ "0badbeef" ]] ; then
echo $i
break
let i+=1
done
I tried to run this script and am getting the error stated above.

You have to close the if with fi.
Paste your script on https://www.shellcheck.net/

Related

syntax error `(" unexpected in bash script [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 2 years ago.
i'm coding a simple bash script and i found this error syntax error at line XX `(' unexpected
my code:
function myfun(){
echo XXXX
echo YYYY
read choice
}
choice=$(myfun)
where is the error. i used the ShellCheck and no errors were detected.
Make sure you are running the script with bash. That error is a commonly seen dash shell error.
I suspect the first line of your script is not #!/bin/bash, i.e. you may have left out the shebang line entirely resulting in the default shell being used (which will often be dash especially on Debian derived Linuxes where /bin/sh -> dash).
Try running this:
#!/bin/bash
myfun()
{
echo XXXX
echo YYYY
read choice
}
choice=$(myfun)

simple script to remove files in linux bash [duplicate]

This question already has an answer here:
Find "command not found" when executed in bash loop
(1 answer)
Closed 3 years ago.
I made a simple script in linux bash just like bellow:
#!/bin/bash
PATH=/tmp_with_zip_files
FILETYPE=zip
i=1
for filename in $PATH/*.$FILETYPE;
do
echo "rm $filename";
if [ -f $filename ];
then rm $filename;
fi
i=$((i+1))
done
echo "$i files removed"
But, when i run script i have error, because script doesnt work correctly. It's mean from console i have a message:
zip_delete.sh: line 11: rm: command not found
Why linux bash script not recognize linux command rm?
Lol I think it's because you're overwriting the default $PATH variable (which is the variable that tells bash where to look for executables). During execution, it can't find the rm program in PATH because it's pointing to only /tmp_with_zip_files
Use a different variable name for your purposes like chicken_nuggets.
WARNING DON'T DO THE FOLLOWING LMAO PATH=$PATH:/tmp_with_zip_files you could delete a bunch of things from PATH and that would suck really bad
The PATH variable holds the path to OS commands (like rm), don't use that as a variable, name it something else, like path_to_files.

Replace Tilde with $HOME in bash script [duplicate]

This question already has answers here:
Tilde expansion in quotes
(3 answers)
How to manually expand a special variable (ex: ~ tilde) in bash
(19 answers)
Closed 4 years ago.
I've been digging through the internet and I see examples of replacing $HOME with ~, but I'm trying to go the other way (e.g. - replace ~ with $HOME and currently if I try to run this:
if [[ $directory_name = *~* ]]; then
echo "${$directory_name/\~/$HOME}"
fi
to replace it, I get this error:
${$directory_name/\~/$HOME}: bad substitution
I have #!/bin/bash at the top of my script file and when I run it I've been using something like this:
sh test-script.sh
I'm also doing this in terminal on a Mac, so I'm not sure if that has anything to do with it.
Again...new to bash scripting so while this seems logical, I could be going about this all wrong and missing something. Thanks!

Bash: getting error in test "[[ not recognized" [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 2 years ago.
The community is reviewing whether to reopen this question as of yesterday.
I'm working with a bash script that is currently working on a server (RHEL4). I'm developing on my laptop with Ubuntu 10.04, but I don't think the platform is causing the problem.
Here's what's happening:
I have a skeleton script that calls another script that does most of the work. However, it makes calls to getConfig.sh a lot. getConfig.sh basically just parses some command line argument (using getopts) and calls a Java program to parse some XML files. Anyways, getConfig.sh is throwing up lots of errors (but still seems to work).
Here's the message that I'm getting
getconfig.sh: 89: [[: not found
getconfig.sh: 89: [[: not found
getconfig.sh: 94: [[: not found
I get those three errors every time it runs; however, the script completes and the Java code runs.
Here's the relavent code section
parseOptions $*
if [[ "${debugMode}" == "true" ]] ; then
DEBUG="-DDEBUG=true"
echo "${JAVA_HOME}/bin/java ${DEBUG} -Djava.endorsed.dirs=${JAXP_HOME} -jar $(dirname $0)/GetXPath.jar ${XML_File} ${XPath_Query}"
fi
Line 89 is "parseOptions $* and line 94 is "fi"
Thanks for the answers.
If your script is executable and you are executing it like ./getconfig.sh, the first line of your script needs to be:
#!/bin/bash
Without that shebang line, your script will be interpreted by sh which doesn't understand [[ in if statements.
Otherwise, you should run your script like bash getconfig.sh, not sh getconfig.sh. Even if your default shell is bash, scripts run with sh will use a reduced set of bash's features, in order to be more compliant with the POSIX standard. [[ is one of the features that is disabled.
Use:
bash scriptname.sh
instead of:
sh scriptname.sh
If you are checking for equality, shouldn't the if be ?
if [[ "${debugMode}" = "true" ]]; then
....
fi

Space between lines in shellscript being taken as a command [duplicate]

This question already has an answer here:
Why would a correct shell script give a wrapped/truncated/corrupted error message? [duplicate]
(1 answer)
Closed 6 years ago.
I am writing a bash script which is a follows
#!/bin/bash
#getting the environment variable from commandline
environment=$1
echo $environment
Now when I run the script with bash ./bashScript.sh Hello , I get the following errors on line
: command not found line 2
: command not found line 5
I see that both of these lines are space and bash script is thus giving me an error
To solve it I write my script as
#!/bin/bash
#
#getting the environment variable from commandline
environment=$1
#
echo $environment
But it looks kind of messy
Is there any other way to achieve this. Thanks for help in advance.
Your comment add the precision that your lines use DOS end of lines (\r\n) when you dump it with od -xc file. To avoid it, you should make sure that your editor uses Unix end of lines (\n).
To fix it on an existing text file, you can use tr:
tr -d '\r' < dos_file > unix_file

Resources