I am new to shell script, I created a sh file and it works well on the terminal, see the code below:
#!/bin/bash
# Use dmidecode to get version
dmidecode=`dmidecode -t chassis | grep 'Version' | sed -r 's/.*(.{6})/\1/'`
# Use ipmitool to get version
fru=`ipmitool fru | grep 'Chassis Part Number' | sed -r 's/.*(.{6})/\1/'`
# Compare the result
compare_result=0
if [ "$dmidecode" == "$fru" ]; then
compare_result="pass"
else
compare_result="false"
fi
# Create json
printf '"tcresult": {"dmidecode":{"chassis_type":"%s"},"fru":{"chassis_type":"%s"},"compare_result":"%s"}\n' "$dmidecode" "$fru" "$compare_result"
And the outcome is:
"tcresult": {"dmidecode":{"chassis_type":"N42.12"},"fru":{"chassis_type":"N42.12"},"compare_result":"pass"}
However, when I execute the sh file, the error shows below:
[root#localhost ~]# cd Desktop/
[root#localhost Desktop]# ls
avms avms.tar check_chasis.sh
[root#localhost Desktop]# sh check_chasis.sh
: command not foundne 3:
: command not foundne 7:
check_chasis.sh: line 15: syntax error: unexpected end of file
Thanks in advance for any advise or comment. Also see screenshot below
The "foundne" message is due to the fact you have an extra CR (carriage return) followed by a space at the beginning of the lines 3 and 7. The shell tries to execute that CR leading to the error message:
check_chasis.sh: line 3: \r : command not found
which is displayed as:
: command not foundne 3:
Remove it with:
tr -d '\r' < check_chasis.sh > check_chassis.bash
Note that dos2unix cannot fix this issue unless used with the -c mac option which is equivalent to running mac2unix.
Related
Errors:
./themezip: line 8: unexpected EOF while looking for matching `''
./themezip: line 11: syntax error: unexpected end of file
My Code:
cat ~/scripts/script-files/repos.txt | xargs -I % sh -c git clone %' && ls
ls -d ~/themes/* > ~/scripts/script-files/dirs
lines=$(wc --lines ~/scripts/script-files/dirs)
sed s/.$// ~/scripts/script-files/dirs > ~/scripts/script-files/dirs1
paste ~/scripts/script-files/dirs1 ~/scripts/script-files/dirs > ~/scripts/script-files/dirs2
cat ~/scripts/script-files/dirs1 | xargs -I % sh -c 'zip -r -q ~/themes/% ~/themes/%/'
cat ~/scripts/script-files/dirs1 | xargs -I % sh -c 'rm -r ~/themes/%/'
rm ~/scripts/script-files/dirs*
The message should be rather clear: you are missing a '. And at the end of the file, you have not closed the last quotes, so the End Of File is a syntax error.
Finding the place where you missed a quote is sometimes tricky. shellcheck.net can help you find the place where it probably went wrong. Most Linux distributions also have a package shellcheck, which does basically the same.
Shellcheck will give some more hints, like the useless use of cat and the fact that lines is never used.
Personally, I would also put the sed pattern in single quotes, even though shellcheck does not mention it.
And, as #user2182349 stated in the comments, the quote is missing at the first line.
I'm trying to call arguments for running a script with the rsync command. I've tried various forms and this is the most "simple", however I continue to get an error.
(bash.sh)
#! /bin/bash
root_dir = $1
target_dir = $2
rsync -avh -P --stats $root_dir $target_dir
echo "Files Transferred!"
ErrorMessage
test.sh: line 2: root_dir: command not found
test.sh: line 3: target_dir: command not found
Command Line
sh bash.sh ./path_root_dir ./path_target_dir
Using a variable with a space, without using speech marks or a quote on each side can cause an error to occur, especially when the variable is used and there was a space in the value.
Another way you could try is to use an array, the options variable is where the array is, and then it is called in the rsync command below,
directory="/etc"
backupDirectory="/backup"
incrementalBackup="/incremental"
options=(-a -e 'ssh -p 10022' -b --backup-dir="$incrementalBackup" --delete)
# rsync
rsync "${options[#]}" user#server:"$directory" "$backupDirectory"
How do you use a command line argument as a file path and check for file existence in Bash?
I have the simple Bash script test.sh:
#!/bin/bash
set -e
echo "arg1=$1"
if [ ! -f "$1" ]
then
echo "File $1 does not exist."
exit 1
fi
echo "File exists!"
and in the same directory, I have a data folder containing stuff.txt.
If I run ./test.sh data/stuff.txt I see the expected output:
arg1=data/stuff.txt
"File exists!"
However, if I call this script from a second script test2.sh, in the same directory, like:
#!/bin/bash
fn="data/stuff.txt"
./test.sh $fn
I get the mangled output:
arg1=data/stuff.txt
does not exist
Why does the call work when I run it manually from a terminal, but not when I run it through another Bash script, even though both are receiving the same file path? What am I doing wrong?
Edit: The filename does not have spaces. Both scripts are executable. I'm running this on Ubuntu 18.04.
The filename was getting an extra whitespace character added to it as a result of how I was retrieving it in my second script. I didn't note this in my question, but I was retrieving the filename from folder list over SSH, like:
fn=$(ssh -t "cd /project/; ls -t data | head -n1" | head -n1)
Essentially, I wanted to get the filename of the most recent file in a directory on a remote server. Apparently, head includes the trailing newline character. I fixed it by changing it to:
fn=$(ssh -t "cd /project/; ls -t data | head -n1" | head -n1 | tr -d '\n' | tr -d '\r')
Thanks to #bigdataolddriver for hinting at the problem likely being an extra character.
I'm new in ksh world and I have a problem right now with a script. The script under this lines is into the .profile file of a user in a UNIX machine and when I try to connect whith him i get always the error
home/userTest/.profile: syntax error: `if' unmatched
I don't know how to solve this, because I suppose that this scripts defines the prompt for the connected user, and if I have this error the prompt only shows "$"
I tried the command
ksh -n /home/userTest/.profile
and I get the error always in the last line of the file
#!/bin/ksh
# ksh example
if [[$0 = "ksh"]];
then
bash
exit $?
fi
if [[$0 = "-ksh"]];
then
bash --login
exit $?
fi
export LOGIN=$LOGNAME
#prompt config
PS1="$LOGIN#"$(hostname)":$PWD"
if [["$(id -u)" = "0"]];
then
export PS1="$PS1# "
else
export PS1="$PS1> "
fi
#Alias utile
alias ll="ls -la"
#Set any export here
export PATH_EXAMPLE=/home/userTest
export JAVA_HOME=$PATH_EXAMPLE/games/java/current
export PATH=$JAVA_HOME/bin:$PATH
How can I solve this problem ?
Thanks.
I had the same error. Turned out it was due to DOS format newlines (CR-LF) in my *.sh file created in Windows and then transferred to a Linux server.
Commands to convert DOS format newlines (CR-LF) to UNIX format newlines (LF)
In Windows: using Notepad++, as explained here:
From the "Edit" menu, select "EOL Conversion" -> "UNIX/OSX Format".
You can also set the default EOL in notepad++ via "Settings" -> "Preferences" -> "New Document/Default Directory" then select "Unix/OSX" under the Format box.
In UNIX/Linux: using one of the techniques explained here:
Convert DOS to UNIX using sed command:
sed 's/^M$//' input.txt > output.txt
Convert DOS to UNIX using tr command:
tr -d '\r' < input.file > output.file
Convert DOS to UNIX using this Perl one-liner:
perl -pi -e 's/\r\n/\n/g' input.file
Convert DOS to UNIX using dos2unix command:
dos2unix myfile.txt or dos2unix -b myfile.txt (with a backup)
[ Bonus tip ]
Commands to convert UNIX format newlines (LF) to DOS format newlines (CR-LF)
Convert UNIX to DOS using unix2dos command:
unix2dos myfile.txt or unix2dos -b myfile.txt (with a backup)
Convert UNIX to DOS using sed command:
sed 's/$'"/`echo \\\n\\\r`/" input.txt > output.txt (you need those \\\, you do)
I am using following version
version sh (AT&T Research) 93u+ 2012-08-01
I did not received any syntax error for your above code , though there a problem with your if statement condition instead of
if [[$0 = "-ksh"]]
it should be
if [[ $0 == "-ksh" ]]
or
if [[ $0 = "-ksh" ]]
the latter is obsolete
The complete code is as below
#!/bin/ksh
# ksh example
if [[ $0 = "ksh" ]];
then
bash
exit $?
fi
if [[ $0 == "-ksh" ]];
then
bash --login
exit $?
fi
export LOGIN=$LOGNAME
#prompt config
PS1="$LOGIN#"$(hostname)":$PWD"
if [[ "$(id -u)" == "0" ]];
then
export PS1="$PS1# "
else
export PS1="$PS1> "
fi
#Alias utile
alias ll="ls -la"
#Set any export here
export PATH_EXAMPLE=/home/userTest
export JAVA_HOME=$PATH_EXAMPLE/games/java/current
export PATH=$JAVA_HOME/bin:$PATH
You script may be having some unwanted character , try to look out for then using cat -vte
you can also try command dos2unix filename and then run ksh -n
I try to do the script:
#!/bin/bash
IP='192.168.1.1'
fping -c1 -t300 $IP 2>/dev/null 1>/dev/null
if [ "$?" = 0 ]
then
echo "Host found"
else
echo "Host not found"
fi
and i turn it:
pi#raspberrypi ~ $ sh /home/pi/sh/test.sh
/home/pi/sh/test.sh: 9: /home/pi/sh/test.sh: Syntax error: "fi" unexpected (expecting "then")
where is the problem?
You can try
$ dos2unix /home/pi/sh/test.sh
and run it again.
Most probably this is because carriage-return \r in your script. Try run this command to clean-up your script. Just run once. Original file will be backed up.
perl -pi.bak -e 's/\r$//' /home/pi/sh/test.sh
If you are editing the script file with Notepad++ on windows you can convert the EOL from the program menu with
Edit => EOL Conversion => Unix (LF)
if xxx then
commond
fi
Syntax error: “fi” unexpected (expecting “then”)
try it :
if xxx
then
commond
fi
it's ok.
It may be that you saved to the file from an ftp server rather than via nano or other console file edit prog.
Try pasting the code into the (empty) file via nano.
This fixed that exact issue for me.