Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
02-12-2021, 01:46 AM
#1
Member
Registered: Mar 2010
Posts: 458
Rep:
bash if loop produces error
Hello,
The snippet of my shell script is as follows:
Code:
mkdir -p /tmp/aa1
if [ "${consider_kinds}" = "all" ] || [ "${consider_kinds}" = "serviceaccounts" ]
then
echo "Uploading source serviceaccounts"
cd ${HOME}/sync-cluster/${consider_namespace}/serviceaccounts
if [ ! -f *.yaml || ! -f *.yml ]
then
echo "No yaml manifest file found"
else
list_of_files=$(ls -lrt|awk '{print $9}')
curl --user "${deploy_user}:${deploy_user_password}" -XPUT "https://testcomany.com.au/testrepovirtual/Clustersync/" -H "Content-Type: application/json" -T ${list_of_files}
fi
fi
if [ "${consider_kinds}" = "all" ] || [ "${consider_kinds}" = "secrets" ]
then
echo "Uploading source secrets"
cd ${HOME}/sync-cluster/${consider_namespace}/secrets
if [ ! -f *.yaml || ! -f *.yml ]
then
echo "No yaml manifest file found"
else
list_of_files=$(ls -lrt|awk '{print $9}')
curl --user "${deploy_user}:${deploy_user_password}" -XPUT "https://testcomany.com.au/testrepovirtual/Clustersync/" -H "Content-Type: application/json" -T ${list_of_files}
fi
fi
if [ "${consider_kinds}" = "all" ] || [ "${consider_kinds}" = "configmaps" ]
then
echo "Uploading source configmaps"
cd ${HOME}/sync-cluster/${consider_namespace}/configmaps
if [ ! -f *.yaml || ! -f *.yml ]
then
echo "No yaml manifest file found"
else
list_of_files=$(ls -lrt|awk '{print $9}')
curl --user "${deploy_user}:${deploy_user_password}" -XPUT "https://testcomany.com.au/testrepovirtual/Clustersync/" -H "Content-Type: application/json" -T ${list_of_files}
fi
fi
if [ "${consider_kinds}" = "all" ] || [ "${consider_kinds}" = "deployments & services" ]
then
echo "Uploading source deployments"
cd ${HOME}/sync-cluster/${consider_namespace}/deployments
if [ ! -f *.yaml || ! -f *.yml ]
then
echo "No yaml manifest file found"
else
list_of_files=$(ls -lrt|awk '{print $9}')
curl --user "${deploy_user}:${deploy_user_password}" -XPUT "https://testcomany.com.au/testrepovirtual/Clustersync/" -H "Content-Type: application/json" -T ${list_of_files}
fi
cd ${HOME}/sync-cluster/${consider_namespace}/services
if [ ! -f *.yaml || ! -f *.yml ]
then
echo "No yaml manifest file found"
else
list_of_files=$(ls -lrt|awk '{print $9}')
curl --user "${deploy_user}:${deploy_user_password}" -XPUT "https://testcomany.com.au/testrepovirtual/Clustersync/" -H "Content-Type: application/json" -T ${list_of_files}
fi
fi
However, I am getting error like below
Code:
./jenkins-part-compose-structure.sh: line 63: [: missing `]'
+ -f '*.yml' ']'
./jenkins-part-compose-structure.sh: line 63: -f: command not found
What I basically want to do is, if there are YAML or YML file(s) are present then get their names so they can be uploaded to the artifactory.
But if the file is not present I don't want the script to fail, it could be legit not to have any particular resource.
What mistake am I making here?
02-12-2021, 04:18 AM
#2
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041
a missing [
Code:
if [ ! -f *.yaml || ! [ -f *.yml ]
I would suggest you to use shellcheck to analyze your script.
02-12-2021, 10:10 AM
#3
LQ Addict
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316
Also, please don't call the if construct a "loop". Nothing loops here. if runs code conditionally.
02-12-2021, 02:31 PM
#4
Senior Member
Registered: Aug 2016
Posts: 3,345
shellcheck gives lots of errors and suggestions on that code snippet.
02-12-2021, 05:22 PM
#5
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,832
Must be either the deprecated
Code:
if [ ! -f *.yaml -o ! -f *.yml ]
or
Code:
if [ ! -f *.yaml ] || [ ! -f *.yml ]
Still bad, because
one token is expected where the glob matches can generate zero or multiple tokens.
A nice fix is a function:
Code:
file_exists() {
for _i
do [ -f "$_i" ] && return
done
return 1
}
Now it is straight forward
Code:
if ! file_exists *.yaml || ! file_exists *.yml
Last but not least, check the logic!
Doesn't the following make more sense?
Code:
if ! file_exists *.yaml && ! file_exists *.yml
Code:
if ! { file_exists *.yaml || file_exists *.yml; }
Code:
if ! file_exists *.yaml *.yml
Last edited by MadeInGermany; 02-12-2021 at 05:30 PM .
02-12-2021, 07:23 PM
#6
Senior Member
Registered: Aug 2016
Posts: 3,345
shellcheck said that the test cannot use -f with a glob and suggested using a for loop instead. Simple enough fix.
maybe a -e would work.
Code:
if ! [ -e *.yaml ] || ! [ -e *.yml ]
should work, but I did not try it
The for loop could be
Code:
yamlExists=1
for i in *.yaml ; do yamlExists=0 ; done
if [ "yamlExists" == "1" ]; then
.....
fi
and a similar loop and test for the *.yml files
Last edited by computersavvy; 02-12-2021 at 07:44 PM .
02-13-2021, 07:36 AM
#7
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,832
The -e does not improve anything.
A for loop is the straight fix (and preferrable over other solutions like "ls -A" ).
And hiding the loop in a universal function is most elegant
02-13-2021, 08:49 AM
#8
Senior Member
Registered: Apr 2010
Posts: 2,167
My 2 cents:
Quote:
if ls {*.yaml,*.yml} 1> /dev/null 2>&1; then
echo "files do exist"
else
echo "files do not exist"
fi
Last edited by JJJCR; 02-13-2021 at 08:49 AM .
Reason: edit
02-13-2021, 09:33 AM
#9
Senior Member
Registered: Mar 2020
Posts: 3,678
Rep:
@JJJCR . And what if there are *.yaml files, but no *.yml files, or vice versa?
02-13-2021, 10:22 AM
#10
Senior Member
Registered: Apr 2010
Posts: 2,167
Quote:
Originally Posted by
shruggy
@JJJCR . And what if there are *.yaml files, but no *.yml files, or vice versa?
Yes, that statement is "and" and will return none if one is missing.
edit to this one:
Quote:
GNU nano 4.3 testx.sh
#!/bin/sh
if ls | egrep '\.yaml$|\.yml$' 1> /dev/null 2>&1; then
echo "files do exist"
else
echo "files do not exist"
fi
02-13-2021, 10:28 AM
#11
Senior Member
Registered: Mar 2020
Posts: 3,678
Rep:
Code:
if grep -qs . *.yaml *.yml
then echo 'files do exist'
else echo 'files do not exist'
fi
02-13-2021, 10:44 AM
#12
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,832
Cannot say that I like that opening of the files.
At least improve it to not require a character:
Code:
if grep -qs '^' *.yaml *.yml
02-13-2021, 11:09 AM
#13
Senior Member
Registered: Mar 2020
Posts: 3,678
Rep:
Many
grep-like tools (ack, ag, etc.) have the
-g option precisely for this, they're not part of the standard Unix toolbox though. I use them all the time on the command line, but not in scripts.
1 members found this post helpful.
02-13-2021, 11:56 PM
#14
Member
Registered: Mar 2010
Posts: 458
Original Poster
Rep:
Amazing people, Many thanks! Shell check helped a lot.
All times are GMT -5. The time now is 10:56 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News