I am doing command line xcodebuild using a shell script. My problem is if I open and close the xx.xcodeproj file once and then use that command it works well.<
I don't think project.xcworkspace in xx.xcodeproj is important. And it has nothing to do with Cocoapods.
If you create a workspace named x.xcworkspace, create a project named y.xcodeproj and add the project to x.xcworkspace at the same time; then you will find that there is no project.xcworkspace is created under y.xcodeproj directory.
However, if you open y.xcodeproj with Xcode, project.xcworkspace is automatically created by Xcode.